-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution65.java
29 lines (22 loc) · 896 Bytes
/
Solution65.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
// The function accepts an integer array arr of size n as its argument. The function needs to return the index of an equilibrium point in the array, where the sum of elements on the left of the index is equal to the sum of elements on the right of the index. If no equilibrium point exist return -1.
public class Code1{
public static int equlibriumPoint(int arr[]){
int totalSum = 0;
int leftSum = 0;
for(int i=0;i<arr.length;i++){
totalSum += arr[i];
}
for(int i=0;i<=arr.length;i++){
totalSum -= arr[i]; // rightsum calculation
if(totalSum == leftSum){
return i;
}
leftSum += arr[i];
}
return -1;
}
public static void main(String args[]){
int arr[] = {-7, 1, 5, 2, -4, 3, 0};
System.out.println(equlibriumPoint(arr));
}
}