Pairs of Non Coinciding Points
In a given cartesian plane, there are N points. We need to find the Number of Pairs of points(A,B) such that
Point A and Point B do not coincide. ​
Manhattan Distance and the Euclidean Distance between the points should be equal.
Note : Pair of 2 points(A,B) is considered same as Pair of 2 points(B ,A). Manhattan Distance = |x2-x1|+|y2-y1|
Euclidean Distance = ((x2-x1)^2 + (y2-y1)^2)^0.5 where points are (x1,y1) and (x2,y2).
Input:
First Line Consist of T - number of test cases. For each Test case:- First Line consist of N , Number of points Next line contains N pairs contains two integers Xi and Yi i.e, X coordinate and the Y coordinate of a Point Output:
Print the number of pairs as asked above.
Constraints:
1<=T <= 50
1<=N <= 2*10 ^ 5
0<=(|Xi|, |Yi| ) <= 10^9
Example:
Sample Input :
1
2
1 1
7 5
Sample Output :
0
// A+B=Root(A^2+B^2) => 2AB=0
// A => |x1-x2| & B => |y1-y2|
class GFG {
public static int coincidingPoints(int[][] points, int n) {
Map<Integer, Integer> mapX = new HashMap<>();
Map<Integer, Integer> mapY = new HashMap<>();
Map<String, Integer> set = new HashMap<>();
int ans = 0;
for (int[] point : points) {
String key = Integer.toString(point[0]) + " " + Integer.toString(point[1]);
set.put(key, set.getOrDefault(key, 0) + 1);
mapX.put(point[0], mapX.getOrDefault(point[0], 0) + 1);
// Removing coinciding pairs
ans += mapX.get(point[0]) - set.get(key);
mapY.put(point[1], mapY.getOrDefault(point[1], 0) + 1);
// Removing coinciding pairs
ans += mapY.get(point[1]) - set.get(key);
}
return ans;
}
}
Last updated