Count number of pairs in array

 Same Numbers of 1s - Pairs

Problem Statement:

The program must accept N integers as the input. The c program must form pairs of integers(X,Y) using the given N integers based on the following condition. The Number of 1s in the binary representation of X and Y must be equal. Then the program must print the number of pairs P formed  as the output.

Solution

#include<stdio.h>

#include <stdlib.h>

int call(int x,int y)

{

    int c=0,c1=0,a,b;

    while(x!=0)

    {

        a=x%2;

        if(a==1)

        c++;

        x=x/2;

        

    }

    while(y!=0)

    {

        b=y%2;

        if(b==1)

        c1++;

        y=y/2;

    }

    if(c==c1)

    return 1;

    else

    return 0;

}

int main()

{

int n,count=0,i,j;

scanf("%d",&n);

int a[n];

for(i=0;i<n;i++)

{

    scanf("%d",&a[i]);

}

for(i=0;i<n-1;i++)

{

    for(j=i+1;j<n;j++)

    {

        if(call(a[i],a[j])==1)

        {

            count++;

        }

    }

}

printf("%d",count);

}

Input

5

1 2 3 4 5

Output

4

Output Explanation

The pairs formed which are having an equal number of 1s in their binary representation are given below.

(1,2),(1,4),(2,4) and (3,5)

So the count 4 is printed as output

Input 1

8

15 22 79 70 92 49 35 32

Output 1

7

Rajesh

This Blog will help you to find out the c programs solution easily and also you can able to understand the program.Please follow our blog for more useful updates.

Post a Comment (0)
Previous Post Next Post