You are given an array ‘A’ of length ‘N’, you have to choose an element from any index in this array and delete it. After deleting the element you will get a new array of length ‘N’-1. Your task is to find the number of such arrays of length ‘N’-1 which are good.
Note :
An array is called good if the sum of elements in odd indexes is equal to the sum of elements in even indexes.
For Example :
In array A= [1 2 4 3 6], if we delete A[4]=6, we will get new array B= [1 2 4 3], where B[0] + B[2] = B[1] + B[3] = 5, which means array B is good.
Input Format :
The first line of the input contains ‘T’ denoting the number of test cases.
The first line of each test case contains the three integers N, length of the array.
The second line of each test case contains N space-separated integers of the array A.
Output Format :
For each test case, print an integer denoting the number of good arrays that can be formed.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function
Constraints :
1 <= T <= 5
1 <= N <= 3000
-5000 <= A[i] <= 5000
Where 'A[i]' denotes the 'ith' element of the given array.
Time Limit: 1sec
2
5
1 2 4 3 6
3
1 1 1
1
3
In test case 1:
We have A = [1 2 4 3 6]
If we delete A[0], we will have B= [2 4 3 6], B[0] + B[2] != B[1] + B[3]
If we delete A[1], we will have B= [1 4 3 6], B[0] + B[2] != B[1] + B[3]
If we delete A[2], we will have B= [1 2 3 6], B[0] + B[2] != B[1] + B[3]
If we delete A[3], we will have B= [1 2 4 6], B[0] + B[2] != B[1] + B[3]
If we delete A[3], we will have B= [1 2 4 3], B[0] + B[2] = B[1] + B[3]
Only 1 good array can be formed.
In test case 2:
We have A = [1 1 1]
If we delete A[0], we will have B= [1 1], B[0] = B[1]
If we delete A[1], we will have B= [1 1], B[0] = B[1]
If we delete A[2], we will have B= [1 1], B[0] = B[1]
3 good arrays can be formed.
2
9
2 2 1 1 1 2 1 1 2
7
2 1 2 2 1 1 1
3
1
Delete all index values one by one and make a new array after deleting
Explanation:
In this approach, we will delete all index values one by one and make a new array after deleting. Then in the new array, we just check the sum of elements in even and odd index and compare them.
Algorithm:
O( N^2 ), where N is the length of the array
There are N indexes in array ‘A’, and for each index, we iterate over a new Array of length N-1, thus time complexity is O( N^2 ).
O(N^2), where N is the length of the array
There are N indexes in array ‘A’, and for each index, we are creating a new array ‘B’ of length N-1, thus time complexity is O( N^2 ).