Q58 Pairs of Non Coinciding Points | Practice | GeeksforGeeks
Fri Feb 03 2023 04:53:13 GMT+0000 (Coordinated Universal Time)
Saved by
@Ayush_dabas07
//{ Driver Code Starts
//Initial Template for Java
import java.io.*;
import java.util.*;
class GFG {
public static void main(String args[]) throws IOException {
BufferedReader read =
new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while (t-- > 0) {
int N = Integer.parseInt(read.readLine());
String S1[] = read.readLine().split(" ");
String S2[] = read.readLine().split(" ");
int[] X = new int[N];
int[] Y = new int[N];
for(int i=0; i<N; i++)
{
X[i] = Integer.parseInt(S1[i]);
Y[i] = Integer.parseInt(S2[i]);
}
Solution ob = new Solution();
System.out.println(ob.numOfPairs(X,Y,N));
}
}
}
// } Driver Code Ends
//User function Template for Java
class Solution {
static int numOfPairs(int[] X, int[] Y, int N) {
//make a frequency map to store x , y and x*y(for seeing duplicates)
HashMap<Integer,Integer> x = new HashMap<>();
HashMap<Integer,Integer> y = new HashMap<>();
HashMap<String,Integer> xy = new HashMap<>();
int ans = 0;
for(int i =0 ; i < X.length ; i++){
int x1 = X[i];
int y1 = Y[i];
String num = x1 + "*" + y1;
int xfreq = x.getOrDefault(x1,0);
int yfreq = y.getOrDefault(y1,0);
int xyfreq = xy.getOrDefault(num,0);
ans += xfreq + yfreq - 2*xyfreq;
x.put(x1,xfreq+1);
y.put(y1,yfreq+1);
xy.put(num,xyfreq+1);
}
return ans;
}
};
content_copyCOPY
https://practice.geeksforgeeks.org/problems/pairs-of-non-coinciding-points4141/1
Comments