The program to find the equilibrium index of an array is discussed here. The equilibrium index of an array is an index such that
The sum of elements at lower indexes = Sum of elements at higher indexes.
For example, consider the array a[] = {-7, 1, 5, 2, -4, 3, 0}. Here, a[0] + a[1] + a[2] = a[4] + a[5] + a[6]. Hence, 3 is the equilibrium index.
@@coding::1@@
Time complexity: O(n^2)
Find the sum of the array and then traverse through the array and keep updating the left sum which is initially zero. To get the right sum, subtract the array values from the sum while traversing. Check the left sum and right sum at each step. If they are equal, return the current index.
@@coding::2@@
Time complexity: O(n)
Recommended Programs