-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathSymmetricPairs.java
35 lines (32 loc) · 1.07 KB
/
SymmetricPairs.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package Problems_on_Arrays;
import java.util.HashMap;
import java.util.Scanner;
/*
* Two pairs (a, b) and (c, d) are said to be symmetric if c is equal to b and a is equal to d.
*/
public class SymmetricPairs {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter the number of pairs");
int n=sc.nextInt();
int[][] arr=new int[n][2];
for (int i=0;i<n;i++){
for (int j=0;j<2;j++){
arr[i][j]=sc.nextInt();
}
}
//creating a hashmap to store the elements so that we check if it is present in map or not
HashMap<Integer,Integer> map=new HashMap<>();
System.out.println("the symetric pairs are ");
for (int i=0;i<n;i++){
int first=arr[i][0];
int second=arr[i][1];
if (map.containsKey(second) && map.get(second)==first){
System.out.print("("+first+","+second+") ");
}else{
map.put(first,second);
}
}
sc.close();
}
}