Sum of difference of bits
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t-->0)
{
int n=ab.nextInt();
int sum=0;
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=ab.nextInt();
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
int c1=0,c2=0;
int temp=arr[i];
int temp2=arr[j];
while(temp!=0 || temp2!=0)
{
if((temp & 1)!=(temp2 & 1))
{
sum++;
}
temp= temp>>1;
temp2=temp2>>1;
}
}
}
System.out.println(2*sum);
}
}
}
import java.lang.*;
import java.io.*;
class GFG
{
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t-->0)
{
int n=ab.nextInt();
int sum=0;
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=ab.nextInt();
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
int c1=0,c2=0;
int temp=arr[i];
int temp2=arr[j];
while(temp!=0 || temp2!=0)
{
if((temp & 1)!=(temp2 & 1))
{
sum++;
}
temp= temp>>1;
temp2=temp2>>1;
}
}
}
System.out.println(2*sum);
}
}
}
0 Comments:
Post a Comment