Find all symmetric pairs in an array

Here given code implementation process.

//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 )


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.

New Comment







© 2021, kalkicode.com, All rights reserved