Challenge: Find Symmetric Pairs in an Array

If you are given a two-dimensional array, can you find a symmetric pair in that array? A solution is placed in the "solution" section to help you, but we would suggest you try to solve it on your own first.

Problem Statement

In this problem, you have to implement the findSymmetric() function to find all the symmetric pairs in the given array. By definition, (a,b) and (c,d) are symmetric pairs if a = d and b = c. An illustration is also provided for your understanding.

Function Prototype:

String findSymmetric(int [][]arr);

Here, arr is a 2D array containing pairs of integers.

Output:

It returns a String containing the first occurance of all of the symmetric pairs of integers in the given array, arr.

Sample Input

arr[][] = [{1, 2}, {3, 4}, {5, 9}, {4, 3}, {9, 5}]

Sample Output

"{3,4}{5,9}"

Note: We will return {3, 4} and {5, 9} instead of {4, 3} and {9, 5} because the former occured first.

Explanation

{ 3 , 4 } has a symmetrical pair to { 4 , 3 } in the given array, Similarly, { 5 , 9 } has a symmetrical pair to { 9 , 5 }.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.