

You are given two arrays, ‘ARR1’ and ‘ARR2’ of size ‘N’ and ‘M’. There are two types of good triplets.
Type 1: Triplet (i, j, k) If the square of ARR1[i] is equal to the product of ARR2[j] and ARR2[k], where 0 <= i <N and 0 <= j < k < M
Type 2: Triplet (i, j, k) If the square of ARR2[i] is equal to the product of ARR1[j] and ARR1[k], where 0 <= i <M and 0 <= j < k < N
Your task is to find the total number of good triplets.
The first line of the input contains an integer, 'T,’ denoting the number of test cases.
The first line of each test case contains two space-separated integers, 'N' and 'M', denoting the number of elements in the array 'ARR1', and the number of elements in the array 'ARR2' respectively.
The second line of each test case contains 'N' space-separated integers denoting the elements of the array 'ARR1'.
The third line of each test case contains 'M' space-separated integers denoting the elements of the array 'ARR2'.
Output Format:
For each test case, print a single line containing a single integer denoting the total number of good triplets following the given conditions.
The output of each test case will be printed in a separate line.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 100
1 <= N <= 100
1 <= M <= 100
1 <= ARR1[i] <= 10^4
1 <= ARR2[i] <= 10^4
Where 'ARR1[i]' denotes the 'i'th' element of the array 'ARR1' and 'ARR2[i]' denotes the 'i'th' element of the array 'ARR2'.
Time limit: 1 sec.
2
3 3
2 6 4
2 2 8
3 3
2 1 4
2 2 2
3
6
For the first test case,
All possible good triplets of type 1 are (0,0,1), (2,0,2) and (2,1,2). There are no possible good triplets of type 2. Hence, the answer is 3 in this case.
For the second test case,
All possible good triplets of type 1 are (0,0,1), (0,0,2) and (0,1,2). All possible good triplets of type 2 are (0,1,2), (1,1,2) and (2,1,2). Hence, the answer is 6 in this case.
2
3 4
2 5 3
2 2 3 3
4 4
1 1 2 2
1 1 1 4
2
15
Generate all possible triplets and check for each triplet if the triplet follows the given conditions.
A simple method is to generate all possible triplets and check for each triplet if the triplet follows the given conditions.
Our approach will be to traverse through ARR1 and in each iteration, we will consider all pairs from ARR2 and check if the element from ARR1 and the pair from ARR2 follow the given conditions of type 1 triplet.
To obtain type 2 triplets, we will traverse through ARR2 and we will consider all pairs from ARR1 and check if the element from ARR2 and the pair from ARR1 follow the given conditions of type 2 triplet.
Algorithm:
O(N * (M ^ 2) + M * (N ^ 2)), where N denotes the number of elements in the array ARR1, M denotes the number of elements in the array ARR2.
We are traversing through the array ARR1, and in each iteration, it takes O(M^2) time to find the good triplets of type 1. Therefore it takes O(N * (M ^ 2)) time. Similarly, to find good triplets of type 2, it takes O(M * (N ^ 2)) time. Hence the overall time complexity is O(N * (M ^ 2) + M * (N ^ 2)).
O(1).
Constant space is being used. Hence, the overall Space Complexity is O(1).