- Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathEqualSide.java
33 lines (28 loc) · 726 Bytes
/
EqualSide.java
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
/**
find an index N where the sum of the integers to the left of N is equal to the sum of the integers to the right of N.
If there is no index that would make this happen, return -1.
I/P:[1,2,3,4,3,2,1]
O/P: 3
*/
publicclassEqualSide {
publicstaticintfindEvenIndex(int[] arr) {
// your code
intsum = 0, eq = 0;
for (inti = 0; i < arr.length; i++){
sum += arr[i];
}
for(inti = 0; i < arr.length; i++){
if (eq == sum - arr[i]){
returni;
}
eq += arr[i];
sum -= arr[i];
}
return -1;
}
publicstaticvoidmain(String[] args){
int[] array = {1,2,3,4,3,2,1};
intsideVal = findEvenIndex(array);
System.out.println(sideVal);
}
}