# Given an array of pairs, find all symmetric pairs in it

Two pairs (a, b) and (c, d) are said to be symmetric if c is equal to b and a is equal to d. For example, (10, 20) and (20, 10) are symmetric. Given an array of pairs find all symmetric pairs in it.

It may be assumed that the first elements of all pairs are distinct.

**Example:**

Input: arr[] = {{11, 20}, {30, 40}, {5, 10}, {40, 30}, {10, 5}} Output: Following pairs have symmetric pairs (30, 40) (5, 10)

**We strongly recommend you to minimize your browser and try this yourself first.**

A **Simple Solution** is to go through every pair, and check every other pair for symmetric. This solution requires O(n^{2}) time.

A **Better Solution** is to use sorting. Sort all pairs by the first element. For every pair, do a binary search for the second element in the given array, i.e., check if the second element of this pair exists as the first element in the array. If found, then compare the first element of pair with the second element. Time Complexity of this solution is O(nLogn).

An **Efficient Solution** is to use Hashing. The first element of pair is used as key and the second element is used as the value. The idea is to traverse all pairs one by one. For every pair, check if its second element is in the hash table. If yes, then compare the first element with the value of the matched entry of the hash table. If the value and the first element match, then we found symmetric pairs. Else, insert the first element as a key and second element as value.

Following is the implementation of this idea.

## C/C++

`#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// A C++ program to find all symmetric pairs in a given array of pairs ` `// Print all pairs that have a symmetric counterpart ` `void` `findSymPairs(` `int` `arr[][2], ` `int` `row) ` `{ ` ` ` `// Creates an empty hashMap hM ` ` ` `unordered_map<` `int` `, ` `int` `> hM; ` ` ` ` ` `// Traverse through the given array ` ` ` `for` `(` `int` `i = 0; i < row; i++) ` ` ` `{ ` ` ` `// First and second elements of current pair ` ` ` `int` `first = arr[i][0]; ` ` ` `int` `sec = arr[i][1]; ` ` ` ` ` `// If found and value in hash matches with first ` ` ` `// element of this pair, we found symmetry ` ` ` `if` `(hM.find(sec) != hM.end() && hM[sec] == first) ` ` ` `cout << ` `"("` `<< sec << ` `", "` `<< first << ` `")"` `<<endl; ` ` ` ` ` `else` `// Else put sec element of this pair in hash ` ` ` `hM[first] = sec; ` ` ` `} ` `} ` ` ` `// Driver method ` `int` `main() ` `{ ` ` ` `int` `arr[5][2]; ` ` ` `arr[0][0] = 11; arr[0][1] = 20; ` ` ` `arr[1][0] = 30; arr[1][1] = 40; ` ` ` `arr[2][0] = 5; arr[2][1] = 10; ` ` ` `arr[3][0] = 40; arr[3][1] = 30; ` ` ` `arr[4][0] = 10; arr[4][1] = 5; ` ` ` `findSymPairs(arr, 5); ` `} ` ` ` `//This is contributed by Chhavi ` |

*chevron_right*

*filter_none*

## Java

`// A Java program to find all symmetric pairs in a given array of pairs ` `import` `java.util.HashMap; ` ` ` `class` `SymmetricPairs { ` ` ` ` ` `// Print all pairs that have a symmetric counterpart ` ` ` `static` `void` `findSymPairs(` `int` `arr[][]) ` ` ` `{ ` ` ` `// Creates an empty hashMap hM ` ` ` `HashMap<Integer, Integer> hM = ` `new` `HashMap<Integer, Integer>(); ` ` ` ` ` `// Traverse through the given array ` ` ` `for` `(` `int` `i = ` `0` `; i < arr.length; i++) ` ` ` `{ ` ` ` `// First and second elements of current pair ` ` ` `int` `first = arr[i][` `0` `]; ` ` ` `int` `sec = arr[i][` `1` `]; ` ` ` ` ` `// Look for second element of this pair in hash ` ` ` `Integer val = hM.get(sec); ` ` ` ` ` `// If found and value in hash matches with first ` ` ` `// element of this pair, we found symmetry ` ` ` `if` `(val != ` `null` `&& val == first) ` ` ` `System.out.println(` `"("` `+ sec + ` `", "` `+ first + ` `")"` `); ` ` ` ` ` `else` `// Else put sec element of this pair in hash ` ` ` `hM.put(first, sec); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver method ` ` ` `public` `static` `void` `main(String arg[]) ` ` ` `{ ` ` ` `int` `arr[][] = ` `new` `int` `[` `5` `][` `2` `]; ` ` ` `arr[` `0` `][` `0` `] = ` `11` `; arr[` `0` `][` `1` `] = ` `20` `; ` ` ` `arr[` `1` `][` `0` `] = ` `30` `; arr[` `1` `][` `1` `] = ` `40` `; ` ` ` `arr[` `2` `][` `0` `] = ` `5` `; arr[` `2` `][` `1` `] = ` `10` `; ` ` ` `arr[` `3` `][` `0` `] = ` `40` `; arr[` `3` `][` `1` `] = ` `30` `; ` ` ` `arr[` `4` `][` `0` `] = ` `10` `; arr[` `4` `][` `1` `] = ` `5` `; ` ` ` `findSymPairs(arr); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# A Python3 program to find all symmetric ` `# pairs in a given array of pairs. ` ` ` `# Print all pairs that have ` `# a symmetric counterpart ` `def` `findSymPairs(arr, row): ` ` ` ` ` `# Creates an empty hashMap hM ` ` ` `hM ` `=` `dict` `() ` ` ` ` ` `# Traverse through the given array ` ` ` `for` `i ` `in` `range` `(row): ` ` ` ` ` `# First and second elements ` ` ` `# of current pair ` ` ` `first ` `=` `arr[i][` `0` `] ` ` ` `sec ` `=` `arr[i][` `1` `] ` ` ` ` ` `# If found and value in hash matches with first ` ` ` `# element of this pair, we found symmetry ` ` ` `if` `(sec ` `in` `hM.keys() ` `and` `hM[sec] ` `=` `=` `first): ` ` ` `print` `(` `"("` `, sec,` `","` `, first, ` `")"` `) ` ` ` ` ` `else` `: ` `# Else put sec element of ` ` ` `# this pair in hash ` ` ` `hM[first] ` `=` `sec ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `arr ` `=` `[[` `0` `for` `i ` `in` `range` `(` `2` `)] ` ` ` `for` `i ` `in` `range` `(` `5` `)] ` ` ` `arr[` `0` `][` `0` `], arr[` `0` `][` `1` `] ` `=` `11` `, ` `20` ` ` `arr[` `1` `][` `0` `], arr[` `1` `][` `1` `] ` `=` `30` `, ` `40` ` ` `arr[` `2` `][` `0` `], arr[` `2` `][` `1` `] ` `=` `5` `, ` `10` ` ` `arr[` `3` `][` `0` `], arr[` `3` `][` `1` `] ` `=` `40` `, ` `30` ` ` `arr[` `4` `][` `0` `], arr[` `4` `][` `1` `] ` `=` `10` `, ` `5` ` ` `findSymPairs(arr, ` `5` `) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find all symmetric ` `// pairs in a given array of pairs ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `public` `class` `SymmetricPairs ` `{ ` ` ` ` ` `// Print all pairs that have a symmetric counterpart ` ` ` `static` `void` `findSymPairs(` `int` `[,]arr) ` ` ` `{ ` ` ` `// Creates an empty hashMap hM ` ` ` `Dictionary<` `int` `,` `int` `> hM = ` `new` `Dictionary<` `int` `,` `int` `>(); ` ` ` `int` `val = 0; ` ` ` ` ` `// Traverse through the given array ` ` ` `for` `(` `int` `i = 0; i < arr.GetLength(0); i++) ` ` ` `{ ` ` ` `// First and second elements of current pair ` ` ` `int` `first = arr[i, 0]; ` ` ` `int` `sec = arr[i, 1]; ` ` ` ` ` `// Look for second element of this pair in hash ` ` ` `if` `(hM.ContainsKey(sec)) ` ` ` `val = hM[sec]; ` ` ` ` ` ` ` `// If found and value in hash matches with first ` ` ` `// element of this pair, we found symmetry ` ` ` `if` `(val != 0 && val == first) ` ` ` `Console.WriteLine(` `"("` `+ sec + ` `", "` `+ first + ` `")"` `); ` ` ` ` ` `else` `// Else put sec element of this pair in hash ` ` ` `hM.Add(first, sec); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(String []arg) ` ` ` `{ ` ` ` `int` `[,]arr = ` `new` `int` `[5, 2]; ` ` ` `arr[0, 0] = 11; arr[0, 1] = 20; ` ` ` `arr[1, 0] = 30; arr[1, 1] = 40; ` ` ` `arr[2, 0] = 5; arr[2, 1] = 10; ` ` ` `arr[3, 0] = 40; arr[3, 1] = 30; ` ` ` `arr[4, 0] = 10; arr[4, 1] = 5; ` ` ` `findSymPairs(arr); ` ` ` `} ` `} ` ` ` `// This code has been contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

Following pairs have symmetric pairs (30, 40) (5, 10)

Time Complexity of this solution is O(n) under the assumption that hash search and insert methods work in O(1) time.

This article is contributed by **Shivam Agrawal**. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Given an array A[] and a number x, check for pair in A[] with sum as x
- Find the Number Occurring Odd Number of Times
- Find the smallest window in a string containing all characters of another string
- Find Itinerary from a given list of tickets
- Find whether an array is subset of another array | Added Method 3
- Find four elements that sum to a given value | Set 2 ( O(n^2Logn) Solution)
- Find the first non-repeating character from a stream of characters
- Count all distinct pairs with difference equal to k
- Find if there is a subarray with 0 sum
- Find the first repeating element in an array of integers
- Check if a given array contains duplicate elements within k distance from each other
- Find the longest substring with k unique characters in a given string
- Group multiple occurrence of array elements ordered by first occurrence
- Find number of Employees Under every Employee
- Find the length of largest subarray with 0 sum