Count of even and odd set bit with array element after XOR with K

Given an array arr[] and a number K. The task is to find the count of the element having odd and even number of the set-bit after taking XOR of K with every element of the given arr[].
Examples:Â
Â
Input: arr[] = {4, 2, 15, 9, 8, 8}, K = 3Â
Output: Even = 2, Odd = 4Â
Explanation:Â
The binary representation of the element after taking XOR with K are:Â
4 ^ 3 = 7 (111)Â
2 ^ 3 = 1 (1)Â
15 ^ 3 = 12 (1100)Â
9 ^ 3 = 10 (1010)Â
8 ^ 3 = 11 (1011)Â
8 ^ 3 = 11 (1011)Â
No of elements with even no of 1’s in binary representation : 2 (12, 10)Â
No of elements with odd no of 1’s in binary representation : 4 (7, 1, 11, 11)
Input: arr[] = {4, 2, 15, 9, 8, 8}, K = 6Â
Output: Even = 4, Odd = 2Â
Â
Â
Naive Approach: The naive approach is to take bitwise XOR of K with each element of the given array arr[] and then, count the set-bit for each element in the array after taking XOR with K.
Time Complexity: O(N*K)
Auxiliary space: O(1)
Efficient Approach:Â
With the help of the following observation, we have:Â
Â
For Example:Â
If A = 4 and K = 3Â
Binary Representation:Â
A = 100Â
K = 011Â
A^K = 111Â
Therefore, the XOR of number A(which has an odd number of set-bit) with the number K(which has an even number of set-bit) results in an odd number of set-bit.Â
And If A = 4 and K = 7Â
Binary Representation:Â
A = 100Â
K = 111Â
A^K = 011Â
Therefore, the XOR of number A(which has an odd number of set-bit) with the number K(which has an odd number of set-bit) results in an even number of set-bit.Â
Â
From the above observations:Â
Â
- If K has an odd number of set-bit, then the count of elements of array arr[] with even set-bit and odd set-bit after taking XOR with K, will be same as the count of even set-bit and odd set-bit int the array before XOR.
- If K has an even number of set-bit, then the count of elements of array arr[] with even set-bit and odd set-bit after taking XOR with K, will reverse as the count of even set-bit and odd set-bit in the array before XOR.
Steps:Â
Â
- Store the count of elements having even set-bit and odd set-bit from the given array arr[].
- If K has odd set-bit, then the count of even and odd set-bit after XOR with K will be the same as the count of even and odd set-bit calculated above.
- If K has even set-bit, then the count of even and odd set-bit after XOR with K will be the count of odd and even set-bit calculated above.
Below is the implementation of the above approach:Â
Â
C++
// C++ program to count the set// bits after taking XOR with a// number K#include <bits/stdc++.h>using namespace std;Â
// Function to store EVEN and odd variablevoid countEvenOdd(int arr[], int n, int K){Â Â Â Â int even = 0, odd = 0;Â
    // Store the count of even and    // odd set bit    for (int i = 0; i < n; i++) {Â
        // Count the set bit using        // in built function        int x = __builtin_popcount(arr[i]);        if (x % 2 == 0)            even++;        else            odd++;    }Â
    int y;Â
    // Count of set-bit of K    y = __builtin_popcount(K);Â
    // If y is odd then, count of    // even and odd set bit will    // be interchanged    if (y & 1) {        cout << "Even = " << odd             << ", Odd = " << even;    }Â
    // Else it will remain same as    // the original array    else {        cout << "Even = " << even             << ", Odd = " << odd;    }}Â
// Driver's Codeint main(void){Â Â Â Â int arr[] = { 4, 2, 15, 9, 8, 8 };Â Â Â Â int K = 3;Â Â Â Â int n = sizeof(arr) / sizeof(arr[0]);Â
    // Function call to count even    // and odd    countEvenOdd(arr, n, K);    return 0;} |
Java
// Java program to count the set// bits after taking XOR with a// number Kclass GFG {Â
         /* Function to get no of set     bits in binary representation     of positive integer n */    static int __builtin_popcount(int n)     {         int count = 0;         while (n > 0) {             count += n & 1;             n >>= 1;         }         return count;     }         // Function to store EVEN and odd variable    static void countEvenOdd(int arr[], int n, int K)    {        int even = 0, odd = 0;             // Store the count of even and        // odd set bit        for (int i = 0; i < n; i++) {                 // Count the set bit using            // in built function            int x = __builtin_popcount(arr[i]);            if (x % 2 == 0)                even++;            else                odd++;        }             int y;             // Count of set-bit of K        y = __builtin_popcount(K);             // If y is odd then, count of        // even and odd set bit will        // be interchanged        if ((y & 1) != 0) {            System.out.println("Even = "+ odd + ", Odd = " + even);        }             // Else it will remain same as        // the original array        else {            System.out.println("Even = " + even + ", Odd = " + odd);        }    }         // Driver's Code    public static void main (String[] args)    {        int arr[] = { 4, 2, 15, 9, 8, 8 };        int K = 3;        int n = arr.length;             // Function call to count even        // and odd        countEvenOdd(arr, n, K);    }  }// This code is contributed by Yash_R |
Python3
# Python3 program to count the set # bits after taking XOR with a # number K Â
# Function to store EVEN and odd variable def countEvenOdd(arr, n, K) : Â
    even = 0; odd = 0; Â
    # Store the count of even and     # odd set bit     for i in range(n) :Â
        # Count the set bit using         # in built function         x = bin(arr[i]).count('1');         if (x % 2 == 0) :            even += 1;         else :            odd += 1; Â
    # Count of set-bit of K     y = bin(K).count('1'); Â
    # If y is odd then, count of     # even and odd set bit will     # be interchanged     if (y & 1) :        print("Even =",odd ,", Odd =", even); Â
    # Else it will remain same as     # the original array     else :        print("Even =" , even ,", Odd =", odd); Â
Â
# Driver's Code if __name__ == "__main__" :Â Â Â Â Â Â Â Â Â arr = [ 4, 2, 15, 9, 8, 8 ]; Â Â Â Â K = 3; Â Â Â Â n = len(arr); Â
    # Function call to count even     # and odd     countEvenOdd(arr, n, K);      # This code is contributed by Yash_R |
C#
// C# program to count the set// bits after taking XOR with a// number Kusing System;Â
public class GFG {Â
         /* Function to get no of set     bits in binary representation     of positive integer n */    static int __builtin_popcount(int n)     {         int count = 0;         while (n > 0) {             count += n & 1;             n >>= 1;         }         return count;     }         // Function to store EVEN and odd variable    static void countEvenOdd(int []arr, int n, int K)    {        int even = 0, odd = 0;             // Store the count of even and        // odd set bit        for (int i = 0; i < n; i++) {                 // Count the set bit using            // in built function            int x = __builtin_popcount(arr[i]);            if (x % 2 == 0)                even++;            else                odd++;        }             int y;             // Count of set-bit of K        y = __builtin_popcount(K);             // If y is odd then, count of        // even and odd set bit will        // be interchanged        if ((y & 1) != 0) {            Console.WriteLine("Even = "+ odd + ", Odd = " + even);        }             // Else it will remain same as        // the original array        else {            Console.WriteLine("Even = " + even + ", Odd = " + odd);        }    }         // Driver's Code    public static void Main (string[] args)    {        int []arr = { 4, 2, 15, 9, 8, 8 };        int K = 3;        int n = arr.Length;             // Function call to count even        // and odd        countEvenOdd(arr, n, K);    }  }// This code is contributed by Yash_R |
Javascript
<script>// Javascript program to count the set// bits after taking XOR with a// number KÂ
/* Function to get no of setbits in binary representationof positive integer n */function __builtin_popcount(n) {Â Â Â Â let count = 0;Â Â Â Â while (n > 0) {Â Â Â Â Â Â Â Â count += n & 1;Â Â Â Â Â Â Â Â n >>= 1;Â Â Â Â }Â Â Â Â return count;}Â
// Function to store EVEN and odd variablefunction countEvenOdd(arr, n, K) {Â Â Â Â let even = 0, odd = 0;Â
    // Store the count of even and    // odd set bit    for (let i = 0; i < n; i++) {Â
        // Count the set bit using        // in built function        let x = __builtin_popcount(arr[i]);        if (x % 2 == 0)            even++;        else            odd++;    }Â
    let y;Â
    // Count of set-bit of K    y = __builtin_popcount(K);Â
    // If y is odd then, count of    // even and odd set bit will    // be interchanged    if ((y & 1) != 0) {        document.write("Even = " + odd + ", Odd = " + even);    }Â
    // Else it will remain same as    // the original array    else {        document.write("Even = " + even + ", Odd = " + odd);    }}Â
// Driver's CodeÂ
let arr = [4, 2, 15, 9, 8, 8];let K = 3;let n = arr.length;Â
// Function call to count even// and oddcountEvenOdd(arr, n, K);Â
// This code is contributed by _saurabh_jaiswal</script> |
Even = 2, Odd = 4
Â
Time Complexity: O(N)
Auxiliary space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!


