Skip to content

Commit 13cbb22

Browse files
committed
Two-Hundred-Forty-Five Commit: Add Find the Middle Index in Array problem to Prefix Sum Section
1 parent 1d1ae15 commit 13cbb22

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package Prefix_Sum;
2+
3+
// Problem Statement: Find the Middle Index in Array (easy)
4+
// LeetCode Question: 1991. Find the Middle Index in Array
5+
6+
public class Problem_1_Find_The_Middle_Index_In_Array {
7+
public int findMiddleIndex(int[] nums){
8+
int totalSum = 0;
9+
for (int num : nums) {
10+
totalSum += num;
11+
}
12+
int leftSum = 0;
13+
for (int i = 0; i < nums.length; i++) {
14+
int rightSum = totalSum - leftSum - nums[i];
15+
if (leftSum == rightSum) {
16+
return i;
17+
}
18+
leftSum += nums[i];
19+
}
20+
return - 1;
21+
}
22+
}

0 commit comments

Comments
 (0)