|
| 1 | +package tcs.practice.array; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +public class SymmentricPairInArray { |
| 7 | + |
| 8 | + public static void main(String[] args) { |
| 9 | + |
| 10 | + int [][]pairs= {{1,2},{2,1},{4,5},{3,2},{2,3}}; |
| 11 | + |
| 12 | + symmentricpair(pairs); |
| 13 | + } |
| 14 | + |
| 15 | + private static void symmentricpair(int[][] pairs) { |
| 16 | + |
| 17 | + HashMap<Integer,Integer> map = new HashMap<>(); |
| 18 | + |
| 19 | + for(int pair[] :pairs) { |
| 20 | + int first=pair[0]; |
| 21 | + int second=pair[1]; |
| 22 | + |
| 23 | + if(map.containsKey(second)&& map.get(second)==first) { |
| 24 | + System.out.println("("+first+","+second+")"); |
| 25 | + }else { |
| 26 | + map.put(first, second); |
| 27 | + } |
| 28 | + |
| 29 | + } |
| 30 | + |
| 31 | + }} |
| 32 | + |
| 33 | + |
| 34 | + /* int n = 5; |
| 35 | + int arr[][] = {{1, 2}, {2, 1}, {3, 4}, {4, 5}, {5, 4}}; |
| 36 | + System.out.println("The symmetric pairs are: "); |
| 37 | + for (int i = 0; i < n; i++) { |
| 38 | + for (int j = i + 1; j < n; j++) { |
| 39 | + if (arr[j][0] == arr[i][1] && arr[j][1] == arr[i][0]) { |
| 40 | + System.out.print("(" + arr[i][1] + " " + arr[i][0] + ")" + " "); |
| 41 | + break; |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + }Map<Integer, Integer> map = new HashMap<>(); |
| 46 | + for (int[] pair : pairs) { |
| 47 | + int first = pair[0]; |
| 48 | + int second = pair[1]; |
| 49 | +
|
| 50 | + // Check if reverse pair exists |
| 51 | + if (map.containsKey(second) && map.get(second) == first) { |
| 52 | + System.out.println("(" + first + ", " + second + ")"); |
| 53 | + } else { |
| 54 | + map.put(first, second); |
| 55 | + } |
| 56 | +}*/ |
| 57 | + |
| 58 | +/*HashMap < Integer, Integer > mp = new HashMap < Integer, Integer > (); |
| 59 | + System.out.println("The Symmetric Pairs are: "); |
| 60 | + for (int i = 0; i < arr.length; i++) { |
| 61 | + int first = arr[i][0]; |
| 62 | + int second = arr[i][1]; |
| 63 | + if (mp.get(second) != null && mp.get(second) == first) { |
| 64 | + System.out.print("("+first + " " + second+") "); |
| 65 | + } else { |
| 66 | + mp.put(first, second); |
| 67 | + } |
| 68 | + } |
| 69 | +
|
| 70 | + } |
| 71 | +}*/ |
| 72 | + |
0 commit comments