Given an array arr of N elements and another array Q containing values of K, the task is to print the count of elements in the array arr with odd and even set bits after its XOR with each element K in the array Q.
Examples:
Input: arr[] = { 2, 7, 4, 5, 3 }, Q[] = { 3, 4, 12, 6 }
Output: 2 3
3 2
2 3
2 3
Input: arr[] = { 7, 1, 6, 5, 11 }, Q[] = { 2, 10, 3, 6 }
Output: 3 2
2 3
2 3
2 3
Approach:
- XOR of two elements both having odd or even set bits, results to even set bits.
- XOR of two elements, one having odd and other having even set bits or vice versa, results to odd set bits.
- Precompute count of elements with even and odd set bits of all array elements using Brian Kernighan’s Algorithm.
- For all elements of Q, count number of set bits. If count of set bits is even, the count of even and odd set bits elements remain unchanged. Otherwise reverse the count and display.
Below is the implementation of the above approach:
C++
// C++ Program to count number// of even and odd set bits// elements after XOR with a// given element#include <bits/stdc++.h>using namespace std;void keep_count(int arr[], int& even, int& odd, int N){ // Store the count of set bits int count; for (int i = 0; i < N; i++) { count = 0; // Brian Kernighan's algorithm while (arr[i] != 0) { arr[i] = (arr[i] - 1) & arr[i]; count++; } if (count % 2 == 0) even++; else odd++; } return;}// Function to solve Q queriesvoid solveQueries( int arr[], int n, int q[], int m){ int even_count = 0, odd_count = 0; keep_count(arr, even_count, odd_count, n); for (int i = 0; i < m; i++) { int X = q[i]; // Store set bits in X int count = 0; // Count set bits of X while (X != 0) { X = (X - 1) & X; count++; } if (count % 2 == 0) { cout << even_count << " " << odd_count << "\n"; } else { cout << odd_count << " " << even_count << "\n"; } }}// Driver codeint main(){ int arr[] = { 2, 7, 4, 5, 3 }; int n = sizeof(arr) / sizeof(arr[0]); int q[] = { 3, 4, 12, 6 }; int m = sizeof(q) / sizeof(q[0]); solveQueries(arr, n, q, m); return 0;} |
Java
// Java program to count number// of even and odd set bits// elements after XOR with a// given elementclass GFG{ static int even, odd;static void keep_count(int arr[], int N){ // Store the count of set bits int count; for(int i = 0; i < N; i++) { count = 0; // Brian Kernighan's algorithm while (arr[i] != 0) { arr[i] = (arr[i] - 1) & arr[i]; count++; } if (count % 2 == 0) even++; else odd++; } return;}// Function to solve Q queriesstatic void solveQueries(int arr[], int n, int q[], int m){ even = 0; odd = 0; keep_count(arr, n); for(int i = 0; i < m; i++) { int X = q[i]; // Store set bits in X int count = 0; // Count set bits of X while (X != 0) { X = (X - 1) & X; count++; } if (count % 2 == 0) { System.out.print(even + " " + odd + "\n"); } else { System.out.print(odd + " " + even + "\n"); } }}// Driver codepublic static void main(String[] args){ int arr[] = { 2, 7, 4, 5, 3 }; int n = arr.length; int q[] = { 3, 4, 12, 6 }; int m = q.length; solveQueries(arr, n, q, m);}}// This code is contributed by amal kumar choubey |
Python3
# Python3 program to count number# of even and odd set bits# elements after XOR with a# given elementeven = 0odd = 0def keep_count(arr, N): global even global odd # Store the count of set bits for i in range(N): count = 0 # Brian Kernighan's algorithm while (arr[i] != 0): arr[i] = (arr[i] - 1) & arr[i] count += 1 if (count % 2 == 0): even += 1 else: odd += 1 return# Function to solve Q queriesdef solveQueries(arr, n, q, m): global even global odd keep_count(arr, n) for i in range(m): X = q[i] # Store set bits in X count = 0 # Count set bits of X while (X != 0): X = (X - 1) & X count += 1 if (count % 2 == 0): print(even, odd) else: print(odd, even)# Driver codeif __name__ == '__main__': arr = [ 2, 7, 4, 5, 3 ] n = len(arr) q = [ 3, 4, 12, 6 ] m = len(q) solveQueries(arr, n, q, m)# This code is contributed by samarth |
C#
// C# program to count number// of even and odd set bits// elements after XOR with a// given elementusing System;class GFG{ static int even, odd;static void keep_count(int []arr, int N){ // Store the count of set bits int count; for(int i = 0; i < N; i++) { count = 0; // Brian Kernighan's algorithm while (arr[i] != 0) { arr[i] = (arr[i] - 1) & arr[i]; count++; } if (count % 2 == 0) even++; else odd++; } return;}// Function to solve Q queriesstatic void solveQueries(int []arr, int n, int []q, int m){ even = 0; odd = 0; keep_count(arr, n); for(int i = 0; i < m; i++) { int X = q[i]; // Store set bits in X int count = 0; // Count set bits of X while (X != 0) { X = (X - 1) & X; count++; } if (count % 2 == 0) { Console.Write(even + " " + odd + "\n"); } else { Console.Write(odd + " " + even + "\n"); } }}// Driver codepublic static void Main(){ int []arr = { 2, 7, 4, 5, 3 }; int n = arr.Length; int []q = { 3, 4, 12, 6 }; int m = q.Length; solveQueries(arr, n, q, m);}}// This code is contributed by Code_Mech |
2 3 3 2 2 3 2 3
Time Complexity: O(N * log N)
Recommended Posts:
- Count of even and odd set bit with array element after XOR with K
- Query to count odd and even parity elements in subarray after XOR with K
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Count ways to make Bitwise XOR of odd and even indexed elements equal by removing an array element
- Even numbers at even index and odd numbers at odd index
- Count set bits in the Kth number after segregating even and odd from N natural numbers
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Queries to find the XOR of an Array after replacing all occurrences of X by Y
- Queries for number of array elements in a range with Kth Bit Set
- Find the last remaining element after repeated removal of odd and even indexed elements alternately
- Check if the XOR of an array of integers is Even or Odd
- Minimize difference after changing all odd elements to even
- Queries on XOR of greatest odd divisor of the range
- Queries to check whether bitwise AND of a subarray is even or odd
- Queries to print count of distinct array elements after replacing element at index P by a given element
- Rearrange array such that even index elements are smaller and odd index elements are greater
- Increment odd positioned elements by 1 and decrement even positioned elements by 1 in an Array
- Find Kth element in an array containing odd elements first and then even elements
- Count number of even and odd elements in an array
- Count number of even and odd length elements in an Array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


