You are given an array ‘A’ of length ‘N’. You say an index ‘i’ is beautiful if the sum of the first ‘i - 1’ elements of the array ‘A’ equals the sum of the last ‘N - i’ elements of the array ‘A’, where ‘i’ is in 1-based indexing. Now you wonder which is the leftmost beautiful index.
Note: If you select the first index, then the sum of the prefix will be ‘0’, and if you select the last index, then the sum of the suffix will be ‘0’.
Note: You have to print the index in 1-based indexing.
For example:
Let’s say the array ‘A’ = [1, 3, 1, 5], then if we select index ‘2’, the sum of the prefix is ‘1’, and the sum of the suffix is 1 + 5 = 6. Since the sum is not the same, hence index ‘2’ is not a beautiful index.
First-line contains ‘T’, denoting the number of Test cases.
For each Test case:
The first line contains an integer, ‘N’, denoting the length of the array ‘A’.
The following line contains an array ‘A’ of ‘N’ space-separated integers.
Output Format-
For each test case, you have to print an integer denoting the leftmost beautiful index. If there are no beautiful indexes, then print ‘-1’.
Note :
You don’t need to print anything. It has already been taken care of. Just implement the given function.
1 <= ‘T’ <= 10
1 <= ‘N’ <= 10^5
-10^9 <= A[i] <= 10^9, for 1 <= i <= ‘N’
Note- Sum of ‘N’ over all test cases does not exceed 10^5.
Time Limit: 1 sec
2
3
1 1 1
3
1 2 3
2
-1
For test case 1:
Index ‘2’ is the leftmost beautiful index. The left sum is 1 and the right sum is also 1.
For test case 2:
No index is beautiful.
2
6
1 7 3 6 5 6
3
2 1 -1
4
1
Check each index.
Approach: For each index, check if it is a beautiful index or not. If it is a beautiful index, print the index else, continue.
Algorithm:
O(N ^ 2). Where ‘N’ is the length of the array ‘A’.
For each index, we are iterating in ‘N - 1’ elements of array ‘A’. Hence the overall complexity is O(N ^ 2).
O(1).
We are creating a few variables. Hence the overall complexity is O(1).