We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 1d1ae15 commit 13cbb22Copy full SHA for 13cbb22
src/Prefix_Sum/Problem_1_Find_The_Middle_Index_In_Array.java
@@ -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