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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find count of subsequences
// having odd bitwise AND value
int countSubsequences(vector<int> arr)
{
// Stores count of odd elements
int odd = 0;
// Traverse the array arr[]
for (int x : arr) {
// If x is odd increment count
if (x & 1)
odd++;
}
// Return Answer
return (1 << odd) - 1;
}
// Driver Code
int main()
{
vector<int> arr = { 1, 3, 3 };
// Function Call
cout << countSubsequences(arr);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to find count of subsequences
// having odd bitwise AND value
static int countSubsequences(int arr[], int N)
{
// Stores count of odd elements
int odd = 0;
// Traverse the array arr[]
for (int i = 0; i < N; i++) {
// If x is odd increment count
if ((arr[i] & 1) % 2 == 1)
odd++;
}
// Return Answer
return (1 << odd) - 1;
}
// Driver Code
public static void main(String[] args)
{
int N = 3;
int arr[] = { 1, 3, 3 };
// Function Call
System.out.println(countSubsequences(arr, N));
}
}
// This code is contributed by dwivediyash
Python3
# python program for the above approach
# Function to find count of subsequences
# having odd bitwise AND value
def countSubsequences(arr):
# Stores count of odd elements
odd = 0
# Traverse the array arr[]
for x in arr:
# If x is odd increment count
if (x & 1):
odd = odd + 1
# Return Answer
return (1 << odd) - 1
# Driver Code
if __name__ == "__main__":
arr = [1, 3, 3]
# Function Call
print(countSubsequences(arr))
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to find count of subsequences
// having odd bitwise AND value
static int countSubsequences(int []arr, int N)
{
// Stores count of odd elements
int odd = 0;
// Traverse the array arr[]
for (int i = 0; i < N; i++) {
// If x is odd increment count
if ((arr[i] & 1) % 2 == 1)
odd++;
}
// Return Answer
return (1 << odd) - 1;
}
// Driver Code
public static void Main(string[] args)
{
int N = 3;
int []arr = { 1, 3, 3 };
// Function Call
Console.WriteLine(countSubsequences(arr, N));
}
}
// This code is contributed by AnkThon
JavaScript
<script>
// Javascript program for the above approach
// Function to find count of subsequences
// having odd bitwise AND value
function countSubsequences(arr)
{
// Stores count of odd elements
let odd = 0;
// Traverse the array arr[]
for (let x = 0; x < arr.length; x++) {
// If x is odd increment count
if (arr[x] & 1)
odd++;
}
// Return Answer
return (1 << odd) - 1;
}
// Driver Code
let arr = [ 1, 3, 3 ];
// Function Call
document.write(countSubsequences(arr));
// This code is contributed by subham348.
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Count subsequences having odd Bitwise OR values in an array Given an array arr[] consisting of N positive integers, the task is to find the number of subsequences from the given array whose Bitwise OR value is odd. Examples: Input: arr = [2, 4, 1]Output: 4Explanation: Subsequences with odd Bitwise OR values are {1}, {2, 1}, {4, 1}, {2, 4, 1} Input: arr = [1,
5 min read
Count subsequences having odd Bitwise XOR values from an array 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: 4Explanation: Subsequences with odd Bitwise XOR are {1}, {3}, {1, 4}, {3, 4}. Input: A[] = {2, 8, 6}Output: 0Explanation: No such
5 min read
Count of subsequence of an Array having all unique digits Given an array A containing N positive integers, the task is to find the number of subsequences of this array such that in each subsequence , no digit is repeated twice, i.e. all the digits of the subsequences must be unique.Examples: Input: A = [1, 12, 23, 34] Output: 7 The subsequences are: {1}, {
15+ min read
Increase the count of given subsequences by optimizing the Array Given an array X[] of length N. Which contains the alternate frequency of 0s and 1s in Binary String starting from 0. Then the task is to maximize the count of the "01" subsequence where you can choose two different elements of X[], let's say Xi and Xj such that abs (i - j) = 2, and then swap Xi and
11 min read
Count subsequences with same values of Bitwise AND, OR and XOR We are given an array arr of n element. We need to count number of non-empty subsequences such that these individual subsequences have same values of bitwise AND, OR and XOR. For example, we need to count a subsequence (x, y, z) if (x | y | z) is equal to (x & y & z) and (x ^ y ^ z). For a s
6 min read