Given two arrays A[] and B[] having N and M positive elements respectively. The task is to count the number of elements in array A with even number of set bits in XOR for every element of array B.
Examples:
Input: A[] = { 4, 2, 15, 9, 8, 8 }, B[] = { 3, 4, 22 }
Output: 2 4 4
Explanation:
Binary representation of elements of A are : 100, 10, 1111, 1001, 1000, 1000
Binary representation of elements of B are : 11, 101, 10110
Now for element 3(11),
3^4 = 11^100 = 111
3^2 = 11^10 = 01
3^15 = 11^1111 = 1100
3^9 = 11^1001 = 1111
3^8 = 11^1000 = 1011
3^8 = 11^1000 = 1011
Only 2 elements {15, 9} in A[] are there for element 3 such that count of set bit after XOR is even. So the count is 2.
Similarly, Count for element 4 and 22 is 4.
Input: A[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, B[] = { 4 }
Output: 5
Explanation:
The element in A[] such that count of set bit after XOR is even is {1, 2, 4, 7, 8}. So the count is 5.
Naive Approach: The idea is to compute the XOR for every element in the array B[] with each element in the array A[] and count the number having even set bit.
Approach:
- First we create a function countSetBits which returns the number of set bits in the binary representation of a given integer.
- Then we Iterate through the array B[] and for each element, iterate through the array A[] and count the number of elements in A[] with even number of set bits in XOR with the current element of B[].
- Print the count obtained in step 2 for each element of B[].
Implementation:
C++
#include <bits/stdc++.h> using namespace std; int countSetBits( int n){ int count = 0; while (n){ count += n & 1; n >>= 1; } return count; } void countEvenSetBits( int A[], int B[], int N, int M){ for ( int i=0; i<M; i++){ int count = 0; for ( int j=0; j<N; j++){ if (countSetBits(B[i] ^ A[j]) % 2 == 0){ count++; } } cout << count << " " ; } } int main() { int A[] = { 4, 2, 15, 9, 8, 8 }; int B[] = { 3, 4, 22 }; int N= sizeof (A)/ sizeof (A[0]); int M= sizeof (B)/ sizeof (B[0]); countEvenSetBits(A, B,N,M); return 0; } |
Java
public class CountEvenSetBits { static int countSetBits( int n) { int count = 0 ; while (n != 0 ) { count += n & 1 ; n >>= 1 ; } return count; } static void countEvenSetBits( int [] A, int [] B, int N, int M) { for ( int i = 0 ; i < M; i++) { int count = 0 ; for ( int j = 0 ; j < N; j++) { if (countSetBits(B[i] ^ A[j]) % 2 == 0 ) { count++; } } System.out.print(count + " " ); } } public static void main(String[] args) { int [] A = { 4 , 2 , 15 , 9 , 8 , 8 }; int [] B = { 3 , 4 , 22 }; int N = A.length; int M = B.length; countEvenSetBits(A, B, N, M); } } |
Python3
def countSetBits(n): count = 0 while n: count + = n & 1 n >> = 1 return count def countEvenSetBits(A, B, N, M): for i in range (M): count = 0 for j in range (N): if countSetBits(B[i] ^ A[j]) % 2 = = 0 : count + = 1 print (count, end = " " ) A = [ 4 , 2 , 15 , 9 , 8 , 8 ] B = [ 3 , 4 , 22 ] N = len (A) M = len (B) countEvenSetBits(A, B, N, M) |
C#
using System; class GFG { // Function to count the number of set bits (1s) in an integer. static int CountSetBits( int n) { int count = 0; while (n > 0) { count += n & 1; n >>= 1; } return count; } // Function to count even set bits for each element in B[] compared to A[]. static void CountEvenSetBits( int [] A, int [] B, int N, int M) { for ( int i = 0; i < M; i++) { int count = 0; for ( int j = 0; j < N; j++) { // XOR the current elements of B[i] and A[j], then count the set bits. if (CountSetBits(B[i] ^ A[j]) % 2 == 0) { count++; } } Console.Write(count + " " ); } } static void Main() { int [] A = { 4, 2, 15, 9, 8, 8 }; int [] B = { 3, 4, 22 }; int N = A.Length; int M = B.Length; CountEvenSetBits(A, B, N, M); } } |
Javascript
function countSetBits(n) { let count = 0; while (n) { count += n & 1; n >>= 1; } return count; } function countEvenSetBits(A, B) { for (let i = 0; i < B.length; i++) { let count = 0; for (let j = 0; j < A.length; j++) { if (countSetBits(B[i] ^ A[j]) % 2 === 0) { count++; } } console.log(count + " " ); } } // Driver code let A = [4, 2, 15, 9, 8, 8]; let B = [3, 4, 22]; countEvenSetBits(A, B); |
2 4 4
Time Complexity: O(N*M*log(max(A)+max(B)), where N and M is the length of array A[] and B[] respectively.
Auxiliary Space: O(1) as we are not using any extra space .
Efficient Approach: The idea is to use the property of XOR. For any two numbers, if the count of set bit for both the numbers are even or odd then count of the set bit after XOR of both numbers is even.
Below are the steps based on the above property:
- Count the number of element in the array A[] having even(say a) and odd(say b) number of set bits.
- For each element in the array B[]:
- If current element have even count of set bit, then the number element in the array A[] whose XOR with the current element has even count of set bit is a.
- If current element have odd count of set bit, then the number element in the array A[] whose XOR with the current element has even count of set bit is b.
Below is the implementation of the above approach:
CPP
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function that count the XOR of B[] // with all the element in A[] having // even set bit void countEvenBit( int A[], int B[], int n, int m) { int i, j, cntOdd = 0, cntEven = 0; for (i = 0; i < n; i++) { // Count the set bits in A[i] int x = __builtin_popcount(A[i]); // check for even or Odd if (x & 1) { cntEven++; } else { cntOdd++; } } // To store the count of element for // B[] such that XOR with all the // element in A[] having even set bit int CountB[m]; for (i = 0; i < m; i++) { // Count set bit for B[i] int x = __builtin_popcount(B[i]); // check for Even or Odd if (x & 1) { CountB[i] = cntEven; } else { CountB[i] = cntOdd; } } for (i = 0; i < m; i++) { cout << CountB[i] << ' ' ; } } // Driver Code int main() { int A[] = { 4, 2, 15, 9, 8, 8 }; int B[] = { 3, 4, 22 }; countEvenBit(A, B, 6, 3); return 0; } |
Java
// Java program for the above approach import java.util.*; class GFG{ // Function that count the XOR of B[] // with all the element in A[] having // even set bit static void countEvenBit( int A[], int B[], int n, int m) { int i, j, cntOdd = 0 , cntEven = 0 ; for (i = 0 ; i < n; i++) { // Count the set bits in A[i] int x = Integer.bitCount(A[i]); // check for even or Odd if (x % 2 == 1 ) { cntEven++; } else { cntOdd++; } } // To store the count of element for // B[] such that XOR with all the // element in A[] having even set bit int []CountB = new int [m]; for (i = 0 ; i < m; i++) { // Count set bit for B[i] int x = Integer.bitCount(B[i]); // check for Even or Odd if (x% 2 == 1 ) { CountB[i] = cntEven; } else { CountB[i] = cntOdd; } } for (i = 0 ; i < m; i++) { System.out.print(CountB[i] + " " ); } } // Driver Code public static void main(String[] args) { int A[] = { 4 , 2 , 15 , 9 , 8 , 8 }; int B[] = { 3 , 4 , 22 }; countEvenBit(A, B, 6 , 3 ); } } // This code is contributed by sapnasingh4991 |
Python3
# Python3 program for the above approach # Function that count the XOR of B # with all the element in A having # even set bit def countEvenBit(A, B, n, m): i, j, cntOdd = 0 , 0 , 0 cntEven = 0 for i in range (n): # Count the set bits in A[i] x = bin (A[i])[ 2 :].count( '1' ) # check for even or Odd if (x & 1 ): cntEven + = 1 else : cntOdd + = 1 # To store the count of element for # B such that XOR with all the # element in A having even set bit CountB = [ 0 ] * m for i in range (m): # Count set bit for B[i] x = bin (B[i])[ 2 :].count( '1' ) # check for Even or Odd if (x & 1 ): CountB[i] = cntEven else : CountB[i] = cntOdd for i in range (m): print (CountB[i], end = " " ) # Driver Code if __name__ = = '__main__' : A = [ 4 , 2 , 15 , 9 , 8 , 8 ] B = [ 3 , 4 , 22 ] countEvenBit(A, B, 6 , 3 ) # This code is contributed by mohit kumar 29 |
C#
// C# program for the above approach using System; class GFG{ // Function that count the XOR of []B // with all the element in []A having // even set bit static void countEvenBit( int []A, int []B, int n, int m) { int i, cntOdd = 0, cntEven = 0; for (i = 0; i < n; i++) { // Count the set bits in A[i] int x = bitCount(A[i]); // check for even or Odd if (x % 2 == 1) { cntEven++; } else { cntOdd++; } } // To store the count of element for // []B such that XOR with all the // element in []A having even set bit int []CountB = new int [m]; for (i = 0; i < m; i++) { // Count set bit for B[i] int x = bitCount(B[i]); // check for Even or Odd if (x % 2 == 1) { CountB[i] = cntEven; } else { CountB[i] = cntOdd; } } for (i = 0; i < m; i++) { Console.Write(CountB[i] + " " ); } } static int bitCount( int x) { int setBits = 0; while (x != 0) { x = x & (x - 1); setBits++; } return setBits; } // Driver Code public static void Main(String[] args) { int []A = { 4, 2, 15, 9, 8, 8 }; int []B = { 3, 4, 22 }; countEvenBit(A, B, 6, 3); } } // This code is contributed by Rajput-Ji |
Javascript
<script> // Javascript program for the above approach // Function that count the XOR of B[] // with all the element in A[] having // even set bit function countEvenBit(A, B, n, m) { let i, j, cntOdd = 0, cntEven = 0; for (i = 0; i < n; i++) { // Count the set bits in A[i] let x = bitCount(A[i]); // check for even or Odd if (x & 1) { cntEven++; } else { cntOdd++; } } // To store the count of element for // B[] such that XOR with all the // element in A[] having even set bit let CountB = new Array(m); for (i = 0; i < m; i++) { // Count set bit for B[i] let x = bitCount(B[i]); // check for Even or Odd if (x & 1) { CountB[i] = cntEven; } else { CountB[i] = cntOdd; } } for (i = 0; i < m; i++) { document.write(CountB[i] + " " ); } } function bitCount(x) { let setBits = 0; while (x != 0) { x = x & (x - 1); setBits++; } return setBits; } // Driver Code let A = [ 4, 2, 15, 9, 8, 8 ]; let B = [ 3, 4, 22 ]; countEvenBit(A, B, 6, 3); </script> |
2 4 4
Time Complexity: O(N + M), where N and M are the length of the given two array respectively.
Auxiliary Space: O(M)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!