Posted on by Kalkicode
Code Array

# Find all symmetric pairs in an array

Finding symmetric pairs in an array is a problem in programming where you need to identify pairs of elements that are symmetric with respect to each other.

## Problem Statement

Given an array of pairs (2D array), find all pairs that are symmetric. A pair `(a, b)` is symmetric to pair `(c, d)` if and only if `a` is equal to `d` and `b` is equal to `c`.

## Example

Consider the following 2D array of pairs:

``````arr = {
{0, 3},
{3, 6},
{9, 1},
{2, 4},
{1, 2},
{6, 3},
{7, 1},
{4, 2},
{3, 0}
}``````

The symmetric pairs in this array are:

• `{0, 3}` and `{3, 0}`
• `{3, 6}` and `{6, 3}`
• `{2, 4}` and `{4, 2}`

## Idea to Solve

Iterate through the array and compare each pair `(a, b)` with all other pairs `(c, d)` to check if they are symmetric.

## Pseudocode

``````function symmetric_pairs(arr, size):
for i from 0 to size - 1:
for j from i + 1 to size - 1:
if arr[i][0] == arr[j][1] and arr[i][1] == arr[j][0]:
print("{" + arr[i][0] + "," + arr[i][1] + "} " + "{" + arr[j][0] + "," + arr[j][1] + "}")

// Example usage
arr = { {0, 3}, {3, 6}, {9, 1}, {2, 4}, {1, 2}, {6, 3}, {7, 1}, {4, 2}, {3, 0} }
size = size(arr)
symmetric_pairs(arr, size)``````

## Algorithm Explanation

1. The `symmetric_pairs` function takes the 2D array and its size as parameters.
2. It iterates through each pair `(a, b)` using two nested loops.
3. For each pair `(a, b)`, it compares it with all other pairs `(c, d)` in the array.
4. If `(a, b)` is symmetric to `(c, d)`, it prints both pairs in the required format.

## Code Solution

``````//C Program
//Find all symmetric pairs in an array
#include <stdio.h>

//Display all symmetric pairs in given array
void symmetric_pairs(int arr[][2],int size)
{
for (int i = 0; i < size; ++i)
{
for (int j = i+1; j < size; ++j)
{
//Compare pairs of array elements
if(arr[i][0]==arr[j][1] && arr[i][1]==arr[j][0] )
{
//When get Symmetric pairs
printf("\n{%d,%d} ", arr[i][0],arr[i][1]);
printf("{%d,%d}", arr[j][0],arr[j][1]);
}
}
}
}

int main()
{
//Define 2d array
int arr[][2] = {
{0, 3},
{3, 6},
{9, 1},
{2, 4},
{1, 2},
{6, 3},
{7, 1},
{4, 2},
{3, 0}
};
//Get the size of array
int size=(sizeof(arr)/sizeof(arr[0]));

symmetric_pairs(arr,size);
return 0;
}```
```

#### Output

``````{0,3} {3,0}
{3,6} {6,3}
{2,4} {4,2}``````
``````#include<iostream>

using namespace std;

/*
C++ Program
Find all symmetric pairs in an array
*/
class MyArray {
public:

//Display all symmetric pairs in given array
void symmetric_pairs(int arr[][2], int size) {
for (int i = 0; i < size; ++i) {
for (int j = i + 1; j < size; ++j) {
//Compare pairs of array elements

if (arr[i][0] == arr[j][1] && arr[i][1] == arr[j][0]) {
//When get Symmetric pairs

cout << "\n{" << arr[i][0] << "," << arr[i][1] << "} ";
cout << "{" << arr[j][0] << "," << arr[j][1] << "}";
}
}
}
}
};
int main() {
MyArray obj ;
int arr[][2] = {
{
0,
3
},
{
3,
6
},
{
9,
1
},
{
2,
4
},
{
1,
2
},
{
6,
3
},
{
7,
1
},
{
4,
2
},
{
3,
0
}
};
//Count size of array
int size = sizeof(arr) / sizeof(arr[0]);
obj.symmetric_pairs(arr, size);
return 0;
}```
```

#### Output

``````{0,3} {3,0}
{3,6} {6,3}
{2,4} {4,2}``````
``````/*
Java Program
Find all symmetric pairs in an array
*/
public class MyArray {

//Display all symmetric pairs in given array
public void symmetric_pairs(int [][]arr,int size)
{
for (int i = 0; i < size; ++i)
{
for (int j = i+1; j < size; ++j)
{
//Compare pairs of array elements
if(arr[i][0]==arr[j][1] && arr[i][1]==arr[j][0] )
{
//When get Symmetric pairs
System.out.print("\n"+arr[i][0]+","+arr[i][1]+"} ");
System.out.print("{"+arr[j][0]+","+arr[j][1]+"}" );
}
}
}
}
public static void main(String[] args)
{

MyArray obj = new MyArray();
//Define array elements
int [][]arr =  {
{0, 3},
{3, 6},
{9, 1},
{2, 4},
{1, 2},
{6, 3},
{7, 1},
{4, 2},
{3, 0}
};

//Count size of array
int size=arr.length;

obj.symmetric_pairs(arr,size);

}
}```
```

#### Output

``````{0,3} {3,0}
{3,6} {6,3}
{2,4} {4,2}``````
``````using System;

/*
C# Program
Find all symmetric pairs in an array
*/

public class MyArray {
//Display all symmetric pairs in given array
public void symmetric_pairs(int[,] arr, int size) {
for (int i = 0; i < size; ++i) {
for (int j = i + 1; j < size; ++j) {
//Compare pairs of array elements

if (arr[i,0] == arr[j,1] && arr[i,1] == arr[j,0]) {
Console.Write("\n{" + arr[i,0] + "," + arr[i,1] + "} ");
Console.Write("{" + arr[j,0] + "," + arr[j,1] + "}");
}
}
}
}
public static void Main(String[] args) {
MyArray obj = new MyArray();
//Define array elements
int[,] arr = {
{
0,
3
},
{
3,
6
},
{
9,
1
},
{
2,
4
},
{
1,
2
},
{
6,
3
},
{
7,
1
},
{
4,
2
},
{
3,
0
}
};
//Count size of array
int size = arr.GetLength(0);
obj.symmetric_pairs(arr, size);
}
}```
```

#### Output

``````{0,3} {3,0}
{3,6} {6,3}
{2,4} {4,2}``````
``````<?php
/*
Php Program
Find all symmetric pairs in an array
*/
class MyArray {
//Display all symmetric pairs in given array

public 	function symmetric_pairs(\$arr, \$size) {
for (\$i = 0; \$i < \$size; ++\$i) {
for (\$j = \$i + 1; \$j < \$size; ++\$j) {
//Compare pairs of array elements

if (\$arr[\$i][0] == \$arr[\$j][1] && \$arr[\$i][1] == \$arr[\$j][0]) {
//When get Symmetric pairs

echo("\n{". \$arr[\$i][0] .",". \$arr[\$i][1] ."} ");
echo("{". \$arr[\$j][0] .",". \$arr[\$j][1] ."}");
}
}
}
}
}

function main() {
\$obj = new MyArray();
//Define array elements
\$arr = array(
array(0, 3),
array(3, 6),
array(9, 1),
array(2, 4),
array(1, 2),
array(6, 3),
array(7, 1),
array(4, 2),
array(3, 0)
);
//Count size of array
\$size = count(\$arr);
\$obj->symmetric_pairs(\$arr, \$size);

}
main();```
```

#### Output

``````{0,3} {3,0}
{3,6} {6,3}
{2,4} {4,2}``````
``````/*
Node Js Program
Find all symmetric pairs in an array
*/
class MyArray {
//Display all symmetric pairs in given array
symmetric_pairs(arr, size) {
for (var i = 0; i < size; ++i) {
for (var j = i + 1; j < size; ++j) {
//Compare pairs of array elements

if (arr[i][0] == arr[j][1] && arr[i][1] == arr[j][0]) {
//When get Symmetric pairs

process.stdout.write("\n{" + arr[i][0] + "," + arr[i][1] + "} ");
process.stdout.write("{" + arr[j][0] + "," + arr[j][1] + "}");
}
}
}
}
}

function main(args) {
var obj = new MyArray();
//Define array elements
var arr = [
[0, 3],
[3, 6],
[9, 1],
[2, 4],
[1, 2],
[6, 3],
[7, 1],
[4, 2],
[3, 0]
];
//Count size of array
var size = arr.length;
obj.symmetric_pairs(arr, size);
}

main();```
```

#### Output

``````{0,3} {3,0}
{3,6} {6,3}
{2,4} {4,2}``````
``````#   Python 3 Program
#   Find all symmetric pairs in an array
class MyArray :
# Display all symmetric pairs in given array
def symmetric_pairs(self, arr, size) :
i = 0
while (i < size) :
j = i + 1
while (j < size) :
# Compare pairs of array elements

if (arr[i][0] == arr[j][1] and arr[i][1] == arr[j][0]) :
print("\n(", arr[i][0] ,",", arr[i][1] ,") ", end = "")
print("(", arr[j][0] ,",", arr[j][1] ,")", end = "")

j += 1

i += 1

def main() :
obj = MyArray()
arr = [
[0, 3],
[3, 6],
[9, 1],
[2, 4],
[1, 2],
[6, 3],
[7, 1],
[4, 2],
[3, 0]
]
size = len(arr)
obj.symmetric_pairs(arr, size)

if __name__ == "__main__":
main()```
```

#### Output

``````( 0 , 3 ) ( 3 , 0 )
( 3 , 6 ) ( 6 , 3 )
( 2 , 4 ) ( 4 , 2 )``````
``````#   Ruby Program
#   Find all symmetric pairs in an array
class MyArray
# Display all symmetric pairs in given array
def symmetric_pairs(arr, size)
i = 0
while (i < size)
j = i + 1
while (j < size)
# Compare pairs of array elements

if (arr[i][0] == arr[j][1] && arr[i][1] == arr[j][0])
print("(", arr[i][0] ,",", arr[i][1] ,") ")
print("(", arr[j][0] ,",", arr[j][1] ,")\n")
end
j += 1
end
i += 1
end
end
end
def main()
obj = MyArray.new()
arr = [
[0, 3],
[3, 6],
[9, 1],
[2, 4],
[1, 2],
[6, 3],
[7, 1],
[4, 2],
[3, 0]
]
size = arr.length
obj.symmetric_pairs(arr, size)
end
main()```
```

#### Output

``````(0,3) (3,0)
(3,6) (6,3)
(2,4) (4,2)
``````
``````/*
Scala Program
Find all symmetric pairs in an array
*/
class MyArray {
//Display all symmetric pairs in given array
def symmetric_pairs(arr: Array[Array[Int]], size: Int): Unit = {
var i: Int = 0;
while (i < size) {
var j: Int = i + 1;
while (j < size) {
//Compare pairs of array elements

if (arr(i)(0) == arr(j)(1) && arr(i)(1) == arr(j)(0)) {
print("\n(" + arr(i)(0) + "," + arr(i)(1) + ") ");
print("(" + arr(j)(0) + "," + arr(j)(1) + ")");
}
j += 1;
}
i += 1;
}
}
}
object Main {
def main(args: Array[String]): Unit = {
val obj: MyArray = new MyArray();
val arr: Array[Array[Int]] = Array(
Array(0, 3),
Array(3, 6),
Array(9, 1),
Array(2, 4),
Array(1, 2),
Array(6, 3),
Array(7, 1),
Array(4, 2),
Array(3, 0));
val size: Int = arr.length;
obj.symmetric_pairs(arr, size);
}
}```
```

#### Output

``````(0,3) (3,0)
(3,6) (6,3)
(2,4) (4,2)``````
``````/*
Swift Program
Find all symmetric pairs in an array
*/
class MyArray {
//Display all symmetric pairs in given array
func symmetric_pairs(_ arr: [
[Int]
], _ size: Int) {
var i: Int = 0;
while (i < size) {
var j: Int = i + 1;
while (j < size) {
//Compare pairs of array elements

if (arr[i][0] == arr[j][1] && arr[i][1] == arr[j][0]) {
print("\n(", arr[i][0] ,",", arr[i][1] ,") ", terminator: "");
print("(", arr[j][0] ,",", arr[j][1] ,")", terminator: "");
}
j += 1;
}
i += 1;
}
}
}
func main() {
let obj: MyArray = MyArray();
let arr: [
[Int]
] = [
[0, 3],
[3, 6],
[9, 1],
[2, 4],
[1, 2],
[6, 3],
[7, 1],
[4, 2],
[3, 0]
];
let size: Int = arr.count;
obj.symmetric_pairs(arr, size);
}
main();```
```

#### Output

``````( 0 , 3 ) ( 3 , 0 )
( 3 , 6 ) ( 6 , 3 )
( 2 , 4 ) ( 4 , 2 )``````

## Time Complexity

The algorithm compares each pair with all other pairs, resulting in a nested loop structure. Therefore, the time complexity of the algorithm is `O(n^2)`, where `n` is the size of the 2D array.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post