Find Equilibrium index of an array

preview_player
Показать описание
Equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes.

For example, in an array A:

Input : A[] = {-7, 1, 5, 2, -4, 3, 0}
Output : 3

3 is an equilibrium index, because:

A[0] + A[1] + A[2] = A[4] + A[5] + A[6]

Write a function int equilibrium(int[] arr, int n); that given a sequence arr[] of size n, returns an equilibrium index (if any) or -1 if no equilibrium indexes exist.
Рекомендации по теме
Комментарии
Автор

Apart from index 3, we also have another equilibrium index at 6. This program is just fetching for index 3.

neelkusumekka
Автор

Quite heavy on performance... You can do it in logn

zipzap