Count subsequences having odd Bitwise XOR values from an array
Last Updated :
27 Apr, 2021
Given an array A[] of size N, the task is to count the number of subsequences from the given array whose Bitwise XOR value is odd.
Examples:
Input: A[] = {1, 3, 4}
Output: 4
Explanation: Subsequences with odd Bitwise XOR are {1}, {3}, {1, 4}, {3, 4}.
Input: A[] = {2, 8, 6}
Output: 0
Explanation: No such subsequences are present in the array.
Naive Approach: The simplest approach to solve the problem is to generate all the subsequences of the given array and for each subsequence, check if its Bitwise XOR value is odd or not. If found to be odd, then increase the count by one. After checking for all subsequences, print the count obtained.
Time Complexity: O(N * 2N)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is based on the observation that a subsequence will have odd Bitwise XOR only if the number of odd elements present in it is odd. This is because odd elements have the least significant bit equal to 1. Therefore, XOR of the least significant bits (1^1^1….) up to an odd number of times sets the least significant bit of the new number formed. Therefore, the new number formed is odd.
Follow the steps below to solve the problem:
- Store the count of even and odd elements present in the array A[] in even and odd respectively.
- Traverse the array A[] using the variable i
- If the value of A[i] is odd, increase the value of odd by 1.
- Otherwise, increase the value of even by 1.
- If the value of odd is equal to 0, then print 0 and return.
- Otherwise,
- Find the total combinations with an odd number of odd elements.
- This can be calculated by (XC1+XC3+…+XC(x-(x+1)%2)) * (MC0+MC1+…+MCM) = 2X-1 * 2M = 2X+M-1= 2N-1.
- Therefore, Print 2N-1
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void countSubsequences(vector< int > A)
{
int odd = 0;
int even = 0;
for ( int el : A)
{
if (el % 2 == 1)
odd++;
else
even++;
}
if (odd == 0)
cout << (0);
else
cout << (1 << (A.size() - 1));
}
int main()
{
vector< int > A = { 1, 3, 4 };
countSubsequences(A);
}
|
Java
import java.io.*;
class GFG {
public static void countSubsequences( int [] A)
{
int odd = 0 ;
int even = 0 ;
for ( int el : A) {
if (el % 2 == 1 )
odd++;
else
even++;
}
if (odd == 0 )
System.out.println( 0 );
else
System.out.println( 1 << (A.length - 1 ));
}
public static void main(String[] args)
{
int [] A = { 1 , 3 , 4 };
countSubsequences(A);
}
}
|
Python3
def countSubsequences(A):
odd = 0
even = 0
for el in A:
if (el % 2 = = 1 ):
odd + = 1
else :
even + = 1
if (odd = = 0 ):
print ( 0 )
else :
print ( 1 << len (A) - 1 )
if __name__ = = "__main__" :
A = [ 1 , 3 , 4 ]
countSubsequences(A)
|
C#
using System;
public class GFG
{
public static void countSubsequences( int [] A)
{
int odd = 0;
int even = 0;
foreach ( int el in A) {
if (el % 2 == 1)
odd++;
else
even++;
}
if (odd == 0)
Console.WriteLine(0);
else
Console.WriteLine(1 << (A.Length - 1));
}
public static void Main(String[] args)
{
int [] A = { 1, 3, 4 };
countSubsequences(A);
}
}
|
Javascript
<script>
function countSubsequences( A)
{
var odd = 0;
var even = 0;
for ( var e1 = 0; e1<A.length; e1++)
{
if (A[e1] % 2 == 1)
odd++;
else
even++;
}
if (odd == 0)
document.write(0);
else
document.write((1 << (A.length - 1)));
}
var A = [ 1, 3, 4 ];
countSubsequences(A);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...