You are given a cartesian plane, having ‘N’ points in the form of the array ‘PointX’ and ‘PointY’ where ‘PointX[i]’ and ‘PointY[i]’ represent the ‘X’ coordinates and ‘Y’ coordinates of the i’th point, respectively. You have to find the number of pairs satisfying the following conditions:
1. The points in the pair are distinct.
2. Euclidean distance and the Manhattan distance between the points of the pair should be equal.
Note :
1. Pair (‘P’, ‘Q’) is the same as pair (‘Q’, ‘P’).
2. Euclidean distance is given by: (( ‘X2’ - ‘X1’) ^ 2 + (‘Y2’ - ‘Y1’) ^ 2) ^ 0.5.
3. Manhattan distance is given by: |’X2’ - ‘X1’| + |’Y2’ - ‘Y1’|, where points are (‘X1’, ‘Y1’) and (‘X2’, ‘Y2’).
For example :
Let points be: (1, 2), (2, 3), (1, 3)
The Euclidean distance between points (1, 2) and (1, 3) is: 1
The Manhattan distance between points (1, 2) and (1, 3) is: 1
The Euclidean distance between points (2, 3) and (1, 3) is: 1
The Manhattan distance between points (2, 3) and (1, 3) is: 1
So the pairs can be: [(1, 2), (1, 3)] and [(2, 3), (1, 3)].
So the number of pairs is 2.
The first line of input contains an integer ‘T’, denoting the number of test cases.
The first line of each test case contains a single integer ‘N’, representing the number of points in the cartesian plane.
The second line of each test case contains ‘N’ space-separated integers, representing the point’s ‘X’ coordinates.
The third line of each test case contains ‘N’ space-separated integers, representing the point’s ‘Y’ coordinates.
Output Format :
For each test case, print the number of pairs satisfying the given conditions.
Print output of each test case 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 <= 10
1 <= N <= 10^5
-10^9 <= PointX[i], PointY[i] <= 10^9
Time Limit: 1 sec
2
2
1 7
1 5
3
1 2 1
2 3 3
0
2
For test case 1:
The Euclidean distance between points (1, 1) and (7, 5) is: 10
The Manhattan distance between points (1, 1) and (7, 5) is: 2
Both the distances are not the same, so the number of pairs is 0.
For test case 2:
The Euclidean distance between points (1, 2) and (1, 3) is: 1
The Manhattan distance between points (1, 2) and (1, 3) is: 1
The Euclidean distance between points (2, 3) and (1, 3) is: 1
The Manhattan distance between points (2, 3) and (1, 3) is: 1
So the pairs can be: [(1, 2), (1, 3)] and [(2, 3), (1, 3)].
So the number of pairs is 2.
2
3
1 1 3
1 3 3
2
1 1
2 3
2
1
Try to check conditions for each pair.
The basic idea is to find the count of pairs satisfying the given conditions. We find all the possible pairs and check for the conditions.
Here is the algorithm :
CHECK(‘X1’, ‘Y1’, ‘X2’, ‘Y2’)
O(N^2), where ‘N’ is the number of points.
We iterate the ‘PointX’ and ‘PointY’ array once to traverse the coordinates and then run a nested loop to find the pairs. Therefore, the overall time complexity will be O(N^2).
O(1)
We are not using any extra space. Therefore, the overall space complexity will be O(1).