This is small article about how to find all the equilibrium indexes in integer array. This is very efficient solution. Usually time of execution is O(n^2). This is solution for O(2*n)
@Test
public void verifyThatNumbersAreEquilibrium(){
CodeBase codeBase = new CodeBase();
int[] arrOfNumbers = new int[]{6,1,1,1,1,1,1,1};
//int[] arrOfNumbers = new int[]{1,1,1,1,1,1,1,6};
System.out.print(codeBase.getListOfEqIndexes(arrOfNumbers));
}
public List getListOfEqIndexes(int[] arr) {
List list = new ArrayList();
int tempSumLeft = 0;
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
int sumRight = sum - arr[0];
for (int i = 0; i < arr.length; i++) {
if(i-1 >=0) {
tempSumLeft += arr[i - 1];
}
if (tempSumLeft == sumRight) {
list.add(i);
}
if (i + 1 < arr.length) {
sumRight -= arr[i + 1];
}
}
return list;
}