Count of subsequences having odd Bitwise AND values in the given array
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> |
7
Time Complexity: O(N)
Auxiliary Space: O(1)
Please Login to comment...