Skip to content

Instantly share code, notes, and snippets.

@okplanbo
Created May 14, 2018 14:26
Show Gist options
  • Save okplanbo/0f8b0928fe464f3b7fee42c6af230ae4 to your computer and use it in GitHub Desktop.
Save okplanbo/0f8b0928fe464f3b7fee42c6af230ae4 to your computer and use it in GitHub Desktop.
Finding an equilibrium index in an int array
public int getEquilibrium(int[] array) {
long totalSum = sum(array);
long lowSum = 0L;
for (int i = 0; i < array.length; i++) {
totalSum -= array[i];
if (lowSum == totalSum) {
return i;
}
lowSum += array[i];
}
return -1;
}
public long sum(int[] array) {
return Arrays.stream(array).asLongStream().sum();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment