-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymmentricPairInArray.java
More file actions
72 lines (55 loc) · 1.91 KB
/
SymmentricPairInArray.java
File metadata and controls
72 lines (55 loc) · 1.91 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package tcs.practice.array;
import java.util.HashMap;
import java.util.Map;
public class SymmentricPairInArray {
public static void main(String[] args) {
int [][]pairs= {{1,2},{2,1},{4,5},{3,2},{2,3}};
symmentricpair(pairs);
}
private static void symmentricpair(int[][] pairs) {
HashMap<Integer,Integer> map = new HashMap<>();
for(int pair[] :pairs) {
int first=pair[0];
int second=pair[1];
if(map.containsKey(second)&& map.get(second)==first) {
System.out.println("("+first+","+second+")");
}else {
map.put(first, second);
}
}
}}
/* int n = 5;
int arr[][] = {{1, 2}, {2, 1}, {3, 4}, {4, 5}, {5, 4}};
System.out.println("The symmetric pairs are: ");
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[j][0] == arr[i][1] && arr[j][1] == arr[i][0]) {
System.out.print("(" + arr[i][1] + " " + arr[i][0] + ")" + " ");
break;
}
}
}
}Map<Integer, Integer> map = new HashMap<>();
for (int[] pair : pairs) {
int first = pair[0];
int second = pair[1];
// Check if reverse pair exists
if (map.containsKey(second) && map.get(second) == first) {
System.out.println("(" + first + ", " + second + ")");
} else {
map.put(first, second);
}
}*/
/*HashMap < Integer, Integer > mp = new HashMap < Integer, Integer > ();
System.out.println("The Symmetric Pairs are: ");
for (int i = 0; i < arr.length; i++) {
int first = arr[i][0];
int second = arr[i][1];
if (mp.get(second) != null && mp.get(second) == first) {
System.out.print("("+first + " " + second+") ");
} else {
mp.put(first, second);
}
}
}
}*/