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…
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. The 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:
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]
Input: A[ ] = {1, 2, 3}
Output: -1
From the BPDB Assistant Engineer (CSE) February 2023 – Written (BUET) exam, question 37 of 37.