forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_1991.java
More file actions
24 lines (23 loc) · 655 Bytes
/
_1991.java
File metadata and controls
24 lines (23 loc) · 655 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.fishercoder.solutions;
public class _1991 {
public static class Solution1 {
public int findMiddleIndex(int[] nums) {
int middleIndex = -1;
long sum = 0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
}
long leftSum = 0;
for (int i = 0; i < nums.length; i++) {
sum -= nums[i];
if (i > 0) {
leftSum += nums[i - 1];
}
if (sum == leftSum) {
return i;
}
}
return middleIndex;
}
}
}