C program to find all Symmetric elements in an array

Program to Find all symmetric elements in an array

In this article , we will learn how to create a c program to find all the symmetric elements in an array.The symmetric elements is said to be symmetric when in pairs say (a,b) and (c,d) in which ‘b’ is equal to ‘c’ and ‘a’ is equal to ‘d’ that is , (2,3) is symmetric pairs of (3,2). Given an array of pairs find all symmetric pairs in it.

Symmetric elements in an array

Explanation :

we have given an array pairs , inside that array some symmetric pairs exist. The problem statmenet says that we have to find all symmetric pairs that exist in array. we can simply use two loops and traverse both of the arrays one by one.

Example ,

Input : arr[]={{1,2}.{3,4},{5,6},{2,1},{4,3}}

Output :  (1,2) (3,4

Algorithm

  • Declare two arrays a and b.
  • Take the input array from the user.
  •  Use two loops and traverse both the arrays one by one.
  • if symmetric elements found then print it.
  • At the end we will get our desired output.

C Program to find all symmetric elements in an array

#include<stdio.h>
#include<conio.h>
int main ()
{
int a[100][2], i, j, b[100][2];
int n;

scanf ("%d", &n);

for (i = 0; i < n; i++)
{
for (j = 0; j < 2; j++)
{
scanf ("%d", &a[i][j]);
}
}

int k = 0;
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (a[i][0] == a[j][1] && a[i][1] == a[j][0])
{
b[k][0] = a[i][0];
b[k][1] = a[i][1];
k++;
}
}
}

for (i = 0; i < k; i++)
{
for (j = 0; j < 2; j++)
{
if (j == 0)
{
printf ("(");
}
printf ("%d", b[i][j]);
if (j == 0)
{
printf (",");
}
if (j == 1)
{
printf (") ");
}
}
}
return 0;
}

Output :

Input : arr[]={{1,2}.{3,4},{5,6},{2,1},{4,3}}

Output :  (1,2) (3,4)