Count subsequences having odd Bitwise OR values in an array - GeeksforGeeks

11 Feb, 2022
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, 3, 4]Output: 6
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 OR value is odd or not. If it is odd, then increase the count by one. After checking for all subsequences, print the count obtained.Time Complexity: Auxiliary Space: 
Efficient Approach: Given problem can be solved by observing that for a subsequence to have an odd Bitwise OR value at least one element of the subsequence should be odd. Therefore at least one element in the subsequence should have the least significant digit equal to 1. Follow the steps below to solve this problem:
Store the count of even and odd elements present in the array arr[] in even and odd variables respectively.
Traverse the array A[] using the variable iIf 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 A[i] is odd, increase the value of odd by 1.
Otherwise, increase the value of even by 1.
Total combinations with at least one odd element and any number of even elements can be given by: [2^(odd elements) – 1] * 2^(even elements). Since at least one odd element is needed so empty set of combinations of 1 is excluded
Below is the implementation of the approach:
C++
Java
Python3
C#
Javascript
// C++ implementation for the above approach#include <bits/stdc++.h> using namespace std; // Function to count the subsequences// having odd bitwise OR valueint countSubsequences(vector<int> arr){    // Stores count of odd elements    int odd = 0;     // Stores count of even elements    int even = 0;     // Traverse the array arr[]    for (int x : arr) {         // If element is odd        if (x & 1)            odd++;        else            even++;    }     // Return the final answer    return ((1 << odd) - 1) *              (1 << even);} // Driver Codeint main(){    // Given array arr[]    vector<int> arr = {2, 4, 1};       cout << countSubsequences(arr);}
// Java implementation for the above approachimport java.io.*; class GFG {         // Function to count the subsequences    // having odd bitwise OR value    static int countSubsequences(int arr[])    {               // Stores count of odd elements        int odd = 0;         // Stores count of even elements        int even = 0;         // Traverse the array arr[]        for (int i = 0; i < arr.length; i++) {             // If element is odd            if ((arr[i] & 1) != 0)                odd++;            else                even++;        }         // Return the final answer        return ((1 << odd) - 1) *                  (1 << even);    }     // Driver Code    public static void main (String[] args) {        // Given array arr[]        int arr[] = {2, 4, 1};           System.out.println(countSubsequences(arr));    }} // This code is contributed by Dharanendra L V.
# Python3 implementation for the above approach # Function to count the subsequences# having odd bitwise OR valuedef countSubsequences(arr) :     # Stores count of odd elements    odd = 0;     # Stores count of even elements    even = 0;     # Traverse the array arr[]    for x in arr:         # If element is odd        if (x & 1) :            odd += 1;        else :            even += 1;         # Return the final answer    return ((1 << odd) - 1) * (1 << even);  # Driver Codeif __name__ == "__main__" :     # Given array arr[]    arr = [2, 4, 1];       print(countSubsequences(arr));         # This code is contributed by AnkThon
// Java implementation for the above approachusing System; class GFG {         // Function to count the subsequences    // having odd bitwise OR value    static int countSubsequences(int []arr)    {               // Stores count of odd elements        int odd = 0;         // Stores count of even elements        int even = 0;         // Traverse the array arr[]        for (int i = 0; i < arr.Length; i++) {             // If element is odd            if ((arr[i] & 1) != 0)                odd++;            else                even++;        }         // Return the final answer        return ((1 << odd) - 1) *                  (1 << even);    }     // Driver Code    public static void Main (String[] args) {        // Given array arr[]        int []arr = {2, 4, 1};           Console.Write(countSubsequences(arr));    }} // This code is contributed by shivanisinghss2110
    <script>        // JavaScript Program to implement        // the above approach // Function to count the subsequences// having odd bitwise OR valuefunction countSubsequences( arr){     // Stores count of odd elements    let odd = 0;     // Stores count of even elements    let even = 0;     // Traverse the array arr[]    for (let x of arr) {         // If element is odd        if (x & 1)            odd++;        else            even++;    }     // Return the final answer    return ((1 << odd) - 1) *              (1 << even);} // Driver Code     // Given array arr[]    let arr = [2, 4, 1];       document.write(countSubsequences(arr));      // This code is contributed by Potta Lokesh     </script>
  
4
 
Time Complexity: Auxiliary Space: 
 
dharanendralv23
shivanisinghss2110
lokeshpotta20
ankthon
avtarkumar719
Bitwise-OR
Arrays
Bit Magic
Combinatorial
Arrays
Bit Magic
Combinatorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Next Greater Element
Window Sliding Technique
Count pairs with given sum
Program to find sum of elements in a given array
Reversal algorithm for array rotation
Bitwise Operators in C/C++
Left Shift and Right Shift Operators in C/C++
Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
Count set bits in an integer
Cyclic Redundancy Check and Modulo-2 Division