-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEquilibriumIndex.java
More file actions
50 lines (39 loc) · 1023 Bytes
/
EquilibriumIndex.java
File metadata and controls
50 lines (39 loc) · 1023 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package codility;
/*
* Score 100%
* https://codility.com/demo/results/demoW3GR7Y-TPJ/
*/
class EquilibriumIndex {
public int solution(int[] A) {
if(A.length == 0)
return -1;
else if(A.length ==1)
return 0;
long sum=0;
long bsum[] = new long[A.length];
for(int i=0; i<A.length; i++) {
bsum[i] = sum;
sum +=A[i];
}
for(int i=0; i<A.length; i++) {
long fsum;
if( i<A.length-1) {
fsum = sum-bsum[i]-A[i];
}
else {
fsum = 0;
}
if( fsum == bsum[i] )
return i;
}
return -1;
}
public static void main(String args[]) {
EquilibriumIndex instance = new EquilibriumIndex();
int A[] = {-2147483648};
//int A[] = {2, -1, -2, 1, 500};
//int A[] = {500, 2, -1, -2, 1};
//int A[] = new int[] { -1, 3, -4, 5, 1, -6, 2, 1};
System.err.println(instance.solution(A));
}
}