-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathPivotFinder.java
More file actions
31 lines (22 loc) · 807 Bytes
/
PivotFinder.java
File metadata and controls
31 lines (22 loc) · 807 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
25
26
27
28
29
30
31
package prefix_sum;
import java.util.Arrays;
public class PivotFinder {
public int pivotIndex(int[] nums) {
int total = Arrays.stream(nums).sum();
int leftSum = 0;
for (int i = 0; i < nums.length; i++) {
if (leftSum == total - leftSum - nums[i]) {
return i;
}
leftSum += nums[i];
}
return -1;
}
public static void main(String[] args) {
PivotFinder finder = new PivotFinder();
assert finder.pivotIndex(new int[]{1,7,3,6,5,6}) == 3 : "Test case 1 failed";
assert finder.pivotIndex(new int[]{1,2,3}) == -1 : "Test case 2 failed";
assert finder.pivotIndex(new int[]{2,1,-1}) == 0 : "Test case 3 failed";
System.out.println("All test cases passed!");
}
}