Find Equilibrium index of an array

Опубликовано: 30 Сентябрь 2024
на канале: Kolli Shravani
2,586
29

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.