-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathequilPoint.java
More file actions
43 lines (37 loc) · 1.21 KB
/
equilPoint.java
File metadata and controls
43 lines (37 loc) · 1.21 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
// https://practice.geeksforgeeks.org/problems/equilibrium-point-1587115620/1
public class equilPoint {
public static int equilibriumPoint(long arr[], int n) {
// get sum of arr
// start a left sum, subtract each element from total
// if leftSum == total; that's the equil point!
// Total Array Sum = 1,3,5,2,2 = 13
// Sub elem from Total;
// if leftsum == total; return ind+1
// leftsum + elem
// leftsum = 0
// total-1=12; leftsum+1=1
// total-3=9; leftsum+3=4
// 9-5 = 4;
long arrSum = 0;
long leftSum = 0;
for (long num : arr) {
arrSum += num;
}
for (int i = 0; i < n; i++) {
arrSum -= arr[i];
// System.out.println("leftSum: " + leftSum);
// System.out.println("arrSum: " + arrSum);
if (leftSum == arrSum) {
return i+1;
}
leftSum += arr[i];
}
return -1;
}
public static void main(String[] args) {
long arr[] = {1,3,5,2,2};
int n = 5;
Integer output = equilibriumPoint(arr, n);
System.out.println("Output: " + output); // Output:
}
}