Equilibrium Index
You are given an array Arr consisting of N integers. You need to find the equilibrium index of the array.
An index is considered as an equilibrium index if the sum of elements of the array to the left of that index is equal to the sum of elements to the right of it.
Note:
1. The array follows 0-based indexing, so you need to return the 0-based index of the element.
2. Note that the element at the equilibrium index won’t be considered for either left sum or right sum.
3. If there are multiple indices which satisfy the given condition, then return the left-most index i.e if there are indices i,j,k…. which are equilibrium indices, return the minimum among them
4. If no such index is present in the array, return -1.
Input Format:
The first line of the input contains an integer T, denoting the number of test cases.
The first line of each test case contains the integer N, denoting the size of the array.
The second line of each test case contains N space-separated integers denoting the array elements.
Output Format:
The only line of output of each test case contains a single integer which corresponds to the equilibrium index for the array.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 50
1 <= N <= 10^5
-10^4 <= Arr[i] <= 10^4
Time Limit: 1 sec
CodingNinjas
author
2y
Brute Force
- We can try the brute force approach in which we consider every index starting from 0 as a possible candidate for equilibrium index, and to check if it is really an equilibrium index, we can...read more
CodingNinjas
author
2y
Prefix Sums array
- Since we don’t want to calculate the sum of elements, again and again, we can store the sum of the elements in the form of prefix sums, because to check any index ‘i’, we are only in...read more
CodingNinjas
author
2y
Prefix Sums
- We can even skip the prefix sum array and replace it with some variables. Consider the fact that when we are checking the index i to be equilibrium index, we are only interested in prefSum[...read more
Add answer anonymously...
Top Capgemini Data Analyst interview questions & answers
Popular interview questions of Data Analyst
Top HR questions asked in Capgemini Data Analyst
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app