


You are given 4 arrays 'ARR1’, 'ARR2’, 'ARR3’, 'ARR4’, each consists of ‘N’ numbers. Your task is to return the number of tuples (i, j, k, l) satisfying the following conditions:
0 <= i,j,k,l < ‘N’
ARR1[i] + ARR2[j] + ARR3[k] + ARR4[l] = 0.
The first line of the input contains an integer, 'T,’ denoting the number of test cases.
The first line of each test case contains a single integer, 'N,’ denoting the number of elements in each array.
The next 4 lines of each test case have ‘N’ integers corresponding to the elements of 'ARR1', ‘ARR2’, ‘ARR3’, ‘ARR4’ respectively.
Output Format:
For each test case, return a single integer corresponding to the number of possible tuples.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 500
-100 <= 'ARR1'[i] , ARR2[i], ARR3[i] ,ARR4[i] <= 100
Time limit: 1 sec
2
2
-1 -1
1 1
-1 1
1 -1
1
0
0
0
0
8
1
For the first test case, the possible tuples are:
(0,0,0,0) : ‘ARR1[0]’ + ‘ARR2[0]’ + ‘ARR3[0]’ + ‘ARR4[0]’ = 0.
(0,0,1,1) : ‘ARR1[0]’ + ‘ARR2[0]’ + ‘ARR3[1]’ + ‘ARR4[1]’ = 0.
(0,1,0,0) : ‘ARR1[0]’ + ‘ARR2[1]’ + ‘ARR3[0]’ + ‘ARR4[0]’ = 0.
(0,1,1,1) : ‘ARR1[0]’ + ‘ARR2[1]’ + ‘ARR3[1]’ + ‘ARR4[1]’ = 0.
(1,0,0,0) : ‘ARR1[1]’ + ‘ARR2[0]’ + ‘ARR3[0]’ + ‘ARR4[0]’ = 0.
(1,0,1,1) : ‘ARR1[1]’ + ‘ARR2[0]’ + ‘ARR3[1]’ + ‘ARR4[1]’ = 0.
(1,1,0,0) : ‘ARR1[1]’ + ‘ARR2[1]’ + ‘ARR3[0]’ + ‘ARR4[0]’ = 0.
(1,1,1,1) : ‘ARR1[1]’ + ‘ARR2[1]’ + ‘ARR3[1]’ + ‘ARR4[1]’ = 0.
Hence, the answer is 8.
For the second test case,the possible tuples are:
(0,0,0,0) : ‘ARR1[0]’ + ‘ARR2[0]’ + ‘ARR3[0]’ + ‘ARR4[0]’ = 0.
Hence, the answer is 1.
2
2
1 2
-2 1
-1 2
1 -2
1
1
5
4
-11
3
0
Can you try each possible tuple?
In this approach, we will iterate over all possible tuples and check if the sum of 4 numbers is 0 or not. If they sum up, we will increment ‘AND'. Return the number of tuples in the end.
Algorithm:
O(N^4), where ‘N’ is the number of elements in 'ARR'.
In this approach, we are iterating over all the tuples and the total numbers of tuples are N^4. Hence the overall time complexity is O(N^4).
O(1).
We are using only constant space. Hence the overall space complexity is O(1).