Skip to content

Commit 0b8ddcf

Browse files
authored
Added task 1991.
1 parent 7bc17da commit 0b8ddcf

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g1901_2000.s1991_find_the_middle_index_in_array;
2+
3+
// #Easy #Array #Prefix_Sum #2022_05_18_Time_0_ms_(100.00%)_Space_40.2_MB_(97.29%)
4+
5+
public class Solution {
6+
// TC : O(1), SC: (1)
7+
public int findMiddleIndex(int[] nums) {
8+
// find the sum of all numbers in the array
9+
int sum = 0;
10+
for (int n : nums) {
11+
sum += n;
12+
}
13+
// consider leftSum = 0, rightSum = sum
14+
int leftSum = 0;
15+
int rightSum = sum;
16+
/*
17+
Traverse the array: At each index, subtract the element from rightSum and
18+
check if leftSum equals rightSum. If they do, return the index.
19+
Otherwise, add the number at current index to the leftSum and traverse further.
20+
*/
21+
for (int i = 0; i < nums.length; i++) {
22+
rightSum -= nums[i];
23+
if (leftSum == rightSum) {
24+
return i;
25+
}
26+
leftSum += nums[i];
27+
}
28+
// index not found, return -1
29+
return -1;
30+
}
31+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
1991\. Find the Middle Index in Array
2+
3+
Easy
4+
5+
Given a **0-indexed** integer array `nums`, find the **leftmost** `middleIndex` (i.e., the smallest amongst all the possible ones).
6+
7+
A `middleIndex` is an index where `nums[0] + nums[1] + ... + nums[middleIndex-1] == nums[middleIndex+1] + nums[middleIndex+2] + ... + nums[nums.length-1]`.
8+
9+
If `middleIndex == 0`, the left side sum is considered to be `0`. Similarly, if `middleIndex == nums.length - 1`, the right side sum is considered to be `0`.
10+
11+
Return _the **leftmost**_ `middleIndex` _that satisfies the condition, or_ `-1` _if there is no such index_.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [2,3,-1,8,4]
16+
17+
**Output:** 3
18+
19+
**Explanation:** The sum of the numbers before index 3 is: 2 + 3 + -1 = 4
20+
21+
The sum of the numbers after index 3 is: 4 = 4
22+
23+
**Example 2:**
24+
25+
**Input:** nums = [1,-1,4]
26+
27+
**Output:** 2
28+
29+
**Explanation:** The sum of the numbers before index 2 is: 1 + -1 = 0
30+
31+
The sum of the numbers after index 2 is: 0
32+
33+
**Example 3:**
34+
35+
**Input:** nums = [2,5]
36+
37+
**Output:** -1
38+
39+
**Explanation:** There is no valid middleIndex.
40+
41+
**Constraints:**
42+
43+
* `1 <= nums.length <= 100`
44+
* `-1000 <= nums[i] <= 1000`
45+
46+
**Note:** This question is the same as 724
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g1901_2000.s1991_find_the_middle_index_in_array;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void findMiddleIndex() {
11+
assertThat(new Solution().findMiddleIndex(new int[] {2, 3, -1, 8, 4}), equalTo(3));
12+
}
13+
14+
@Test
15+
void findMiddleIndex2() {
16+
assertThat(new Solution().findMiddleIndex(new int[] {1, -1, 4}), equalTo(2));
17+
}
18+
19+
@Test
20+
void findMiddleIndex3() {
21+
assertThat(new Solution().findMiddleIndex(new int[] {2, 5}), equalTo(-1));
22+
}
23+
}

0 commit comments

Comments
 (0)