Equilibrium index


/ Published in: Java
Save to your folder(s)

Given an array A of N integers, equi(A) is any index i for which:
(1) i is a valid index into A, i.e. 0


Copy this code and paste it in your HTML
  1. int equi(int[] A) {
  2. int i;
  3. int s = 0;
  4. for (i = 0; i < A.length; ++ i) s += A[i];
  5. while (i-- > 0 && A[i] != s) s -= 2 * A[i];
  6. return i;
  7. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.