Skip to content

Instantly share code, notes, and snippets.

@jessedc
Created February 21, 2011 03:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jessedc/836627 to your computer and use it in GitHub Desktop.
Save jessedc/836627 to your computer and use it in GitHub Desktop.
int equi(int A[], int n){
int left = 0;
int right = 0;
int index = -1;
// Sum the array
int i = 0;
for (i = 0; i < n; i++) {
left += A[i];
}
//work backwrds from the end, dividing the array as you go.
for (i = (n - 1); i >= 0; i--){
if ((left - A[i]) == right){
index = i;
break;
}else{
left -= A[i];
right += A[i];
}
}
return index;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment