Count of subsequences having odd Bitwise AND values in the given array
Last Updated :
17 Feb, 2022
Given an array arr[] of N integers, the task is to find the number of subsequences of the given array such that their Bitwise AND value is Odd.
Examples:
Input: arr[] = {2, 3, 1}
Output: 3
Explanation: The subsequences of the given array having odd Bitwise AND values are {3} = 3, {1} = 1, {3, 1} = 3 & 1 = 1.
Input: arr[] = {1, 3, 3}
Output: 7
Recommended: Please try your approach on {IDE} first, before moving on to the solution.
Naive Approach: The given problem can be solved by generating all the subsequences of the given array arr[] and keep track of the count of subsequences such that their Bitwise AND value is odd.
Time Complexity: O(N * 2N)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized by the observation that for the Bitwise AND value of a set of integers to be odd, the least significant bit of each and every element of the set must be a set bit. Therefore, for a subsequence to have an odd Bitwise AND value, all the elements of the subsequence must be odd. Using this observation, the given problem can be solved using the steps below:
- Create a variable odd, which stores the total number of odd integers in the given array arr[]. Initialize it with 0.
- Iterate through the array and increment the value of odd by 1 if the current integer is an odd integer.
- The count of non-empty subsequences of an array having X elements is 2X – 1. Therefore, the number of non-empty subsequences with all odd elements is 2odd – 1, which is the required answer.
Below is the implementation of the approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countSubsequences(vector< int > arr)
{
int odd = 0;
for ( int x : arr) {
if (x & 1)
odd++;
}
return (1 << odd) - 1;
}
int main()
{
vector< int > arr = { 1, 3, 3 };
cout << countSubsequences(arr);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int countSubsequences( int arr[], int N)
{
int odd = 0 ;
for ( int i = 0 ; i < N; i++) {
if ((arr[i] & 1 ) % 2 == 1 )
odd++;
}
return ( 1 << odd) - 1 ;
}
public static void main(String[] args)
{
int N = 3 ;
int arr[] = { 1 , 3 , 3 };
System.out.println(countSubsequences(arr, N));
}
}
|
Python3
def countSubsequences(arr):
odd = 0
for x in arr:
if (x & 1 ):
odd = odd + 1
return ( 1 << odd) - 1
if __name__ = = "__main__" :
arr = [ 1 , 3 , 3 ]
print (countSubsequences(arr))
|
C#
using System;
public class GFG
{
static int countSubsequences( int []arr, int N)
{
int odd = 0;
for ( int i = 0; i < N; i++) {
if ((arr[i] & 1) % 2 == 1)
odd++;
}
return (1 << odd) - 1;
}
public static void Main( string [] args)
{
int N = 3;
int []arr = { 1, 3, 3 };
Console.WriteLine(countSubsequences(arr, N));
}
}
|
Javascript
<script>
function countSubsequences(arr)
{
let odd = 0;
for (let x = 0; x < arr.length; x++) {
if (arr[x] & 1)
odd++;
}
return (1 << odd) - 1;
}
let arr = [ 1, 3, 3 ];
document.write(countSubsequences(arr));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...