


1. The points in the pair are distinct.
2. Euclidean distance and the Manhattan distance between the points of the pair should be equal.
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’).
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.
For each test case, print the number of pairs satisfying the given conditions.
Print output of each test case in a separate line.
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
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’)
We have to find the pairs such that the Manhattan distance and Euclidean distance of the points in the pair are equal. Writing the condition in an equation, we get:
|’X2’ - ‘X1’| + |’Y2’ - ‘Y1’| = (( ‘X2’ - ‘X1’) ^ 2 + (‘Y2’ - ‘Y1’) ^ 2) ^ 0.5
Solving the above equation, we get ‘X1’ = ‘X2’ or ‘Y1’ = ‘Y2’. So we need to find the pairs satisfying either of the conditions.
We can use a map that stores the number of points having their ‘X’ coordinates equal to ‘X[i]’, a map for storing the number of points having their ‘Y’ coordinates equal to ‘Y[i]’, and another map to store the points equal to point (‘X[i]’, ‘Y[i]’).
The number of pairs will be: Number of pairs with same ‘X’ coordinates + Number of pairs with same ‘Y’ coordinates - Number of pairs with coincident points.
Here is the algorithm :