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 equilibrium index if the sum of elements to the left of that index is equal to the sum of elements to the right of it.
Note:
The array follows 0-based indexing, so you need to return the 0- based index of the element. If there are multiple indices which satisfy the given condition, then return the left-most index. If no such index is present in the array, return -1.
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.
1 <= T <= 50
1 <= N <= 10^5
0 <= Arr[i] <= 10^4
Time Limit: 1 sec
1
6
1 7 3 6 5 6
3
The sum of elements to the left of arr[3] = 1 + 7 + 3 = 11.
The sum of elements to the right of arr[3] = 5 + 6 = 11.
Hence the answer is 3.
2
6
1 2 2 9 3 2
4
1 2 3 4
3
-1
In the first test case of sample input 2, the sum of elements on the left of index 3 (arr[3] = 9) is five which is equal to the sum of elements on the right of arr[3].
For the second test case, since no such index exists, we print -1.
Naively find the answer for every index
O(N^2), where N is the size of the array.
O(1), as we are using constant extra memory.