Given an array A[0], A[1], A[2],.....A[N], you have to find the number of pairs (i, j) such that A[i] XOR A[j] is an odd number.
For example:
If the given array is 1, 2, 3 :
1 XOR 2 is 3 and 2 XOR 3 is 1. So, 2 valid pairs.
If the given array is 1, 2, 3, 4 :
1 XOR 2 is 3 and 2 XOR 3 is 1 and 1 XOR 4 is 5 and 3 XOR 4 is 7. So, 4 valid pairs.
Implementation:
For example:
If the given array is 1, 2, 3 :
1 XOR 2 is 3 and 2 XOR 3 is 1. So, 2 valid pairs.
If the given array is 1, 2, 3, 4 :
1 XOR 2 is 3 and 2 XOR 3 is 1 and 1 XOR 4 is 5 and 3 XOR 4 is 7. So, 4 valid pairs.
Implementation:
#include <stdio.h>
#define MAX 1000000
int main()
{
int t,N,A[MAX],count;
/* Number of test cases */
scanf("%d",&t);
while(t--)
{
count=0;
/* Get the number of elements of the array */
scanf("%d",&N);
/* Get the elements of the array */
for(int i=0;i<N;i++)
scanf("%d",&A[i]);
for(int i=0;i<N;i++)
for(int j=i+1;j<N;j++)
/* XOR every possible pairs & check if it is odd, then increment count*/
if((A[i]^A[j])%2!=0)
count++;
/* Print the count for each test case */
printf("%d\n",count);
}
return 0;
}