It's 'Oh, Canada' Week in Our Hate Mail
-
Our pen pals had a one-track mind this week, and their collective mind was
set on Rob Ford, crack, Canada, and the crackstarter. Their thoughts below:
Re...
19 minutes ago
What about the below C++ code? Does that count as a solution? It returns -1, if no such index exists (I have not tested it, though :))
ReplyDelete====
int equilibrium(const int *a, const int n)
{
int sumRight = 0;
for (int i = 0; i < n; ++i) sumRight += a[i];
int j, sumLeft = 0;
for (j = 0; j < n; ++j) {
if (sumLeft == sumRight) break;
sumLeft += a[j];
sumRight -= a[j];
}
return (j == n-1)?-1:j;
}
are you assuming it is sorted?
ReplyDelete