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++
#include <bits/stdc++.h>
using namespace std;
void keep_count( int arr[], int & even,
int & odd, int N)
{
int count;
for ( int i = 0; i < N; i++) {
count = 0;
while (arr[i] != 0) {
arr[i] = (arr[i] - 1) & arr[i];
count++;
}
if (count % 2 == 0)
even++;
else
odd++;
}
return ;
}
void 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];
int count = 0;
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" ;
}
}
}
int 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
class GFG{
static int even, odd;
static void keep_count( int arr[], int N)
{
int count;
for ( int i = 0 ; i < N; i++)
{
count = 0 ;
while (arr[i] != 0 )
{
arr[i] = (arr[i] - 1 ) & arr[i];
count++;
}
if (count % 2 == 0 )
even++;
else
odd++;
}
return ;
}
static 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];
int count = 0 ;
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" );
}
}
}
public 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);
}
}
|
Python3
even = 0
odd = 0
def keep_count(arr, N):
global even
global odd
for i in range (N):
count = 0
while (arr[i] ! = 0 ):
arr[i] = (arr[i] - 1 ) & arr[i]
count + = 1
if (count % 2 = = 0 ):
even + = 1
else :
odd + = 1
return
def solveQueries(arr, n, q, m):
global even
global odd
keep_count(arr, n)
for i in range (m):
X = q[i]
count = 0
while (X ! = 0 ):
X = (X - 1 ) & X
count + = 1
if (count % 2 = = 0 ):
print (even, odd)
else :
print (odd, even)
if __name__ = = '__main__' :
arr = [ 2 , 7 , 4 , 5 , 3 ]
n = len (arr)
q = [ 3 , 4 , 12 , 6 ]
m = len (q)
solveQueries(arr, n, q, m)
|
C#
using System;
class GFG{
static int even, odd;
static void keep_count( int []arr, int N)
{
int count;
for ( int i = 0; i < N; i++)
{
count = 0;
while (arr[i] != 0)
{
arr[i] = (arr[i] - 1) & arr[i];
count++;
}
if (count % 2 == 0)
even++;
else
odd++;
}
return ;
}
static 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];
int count = 0;
while (X != 0)
{
X = (X - 1) & X;
count++;
}
if (count % 2 == 0)
{
Console.Write(even + " " +
odd + "\n" );
}
else
{
Console.Write(odd + " " +
even + "\n" );
}
}
}
public 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);
}
}
|
Javascript
<script>
function keep_count(arr, N)
{
var count;
for (i = 0; i < N; i++)
{
count = 0;
while (arr[i] != 0)
{
arr[i] = (arr[i] - 1) & arr[i];
count++;
}
if (count % 2 == 0)
even++;
else
odd++;
}
return ;
}
function solveQueries(arr, n, q, m)
{
even = 0;
odd = 0;
keep_count(arr, n);
for (i = 0; i < m; i++)
{
var X = q[i];
var count = 0;
while (X != 0)
{
X = (X - 1) & X;
count++;
}
if (count % 2 == 0)
{
document.write(even + " " +
odd + "<br/>" );
}
else
{
document.write(odd + " " +
even + "<br/>" );
}
}
}
var arr = [ 2, 7, 4, 5, 3 ];
var n = arr.length;
var q = [ 3, 4, 12, 6 ];
var m = q.length;
solveQueries(arr, n, q, m);
</script>
|
Time Complexity: O(N * log(max(arr))+m*log(max(q))) where max(arr) and max(q) represents the maximum element in array arr and q respectively.
Auxiliary Space: O(1) as constant space is used
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
25 Apr, 2023
Like Article
Save Article