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: 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++
// C++ program for the above approach #include<bits/stdc++.h> using namespace std; // Function to count the subsequences // having odd bitwise XOR value void countSubsequences(vector< int > A) { // Stores count of odd elements int odd = 0; // Stores count of even elements int even = 0; // Traverse the array A[] for ( int el : A) { // If el is odd if (el % 2 == 1) odd++; else even++; } // If count of odd elements is 0 if (odd == 0) cout << (0); else cout << (1 << (A.size() - 1)); } // Driver Code int main() { // Given array A[] vector< int > A = { 1, 3, 4 }; // Function call to count subsequences // having odd bitwise XOR value countSubsequences(A); } // This code is contributed by mohit kumar 29 |
Java
// Java program for // the above approach import java.io.*; class GFG { // Function to count the subsequences // having odd bitwise XOR value public static void countSubsequences( int [] A) { // Stores count of odd elements int odd = 0 ; // Stores count of even elements int even = 0 ; // Traverse the array A[] for ( int el : A) { // If el is odd if (el % 2 == 1 ) odd++; else even++; } // If count of odd elements is 0 if (odd == 0 ) System.out.println( 0 ); else System.out.println( 1 << (A.length - 1 )); } // Driver Code public static void main(String[] args) { // Given array A[] int [] A = { 1 , 3 , 4 }; // Function call to count subsequences // having odd bitwise XOR value countSubsequences(A); } } |
Python3
# Python3 program for the above approach # Function to count the subsequences # having odd bitwise XOR value def countSubsequences(A): # Stores count of odd elements odd = 0 # Stores count of even elements even = 0 # Traverse the array A[] for el in A: # If el is odd if (el % 2 = = 1 ): odd + = 1 else : even + = 1 # If count of odd elements is 0 if (odd = = 0 ): print ( 0 ) else : print ( 1 << len (A) - 1 ) # Driver Code if __name__ = = "__main__" : # Given array A[] A = [ 1 , 3 , 4 ] # Function call to count subsequences # having odd bitwise XOR value countSubsequences(A) # This code is contributed by ukasp |
C#
// C# program for above approach using System; public class GFG { // Function to count the subsequences // having odd bitwise XOR value public static void countSubsequences( int [] A) { // Stores count of odd elements int odd = 0; // Stores count of even elements int even = 0; // Traverse the array A[] foreach ( int el in A) { // If el is odd if (el % 2 == 1) odd++; else even++; } // If count of odd elements is 0 if (odd == 0) Console.WriteLine(0); else Console.WriteLine(1 << (A.Length - 1)); } // Driver code public static void Main(String[] args) { // Given array A[] int [] A = { 1, 3, 4 }; // Function call to count subsequences // having odd bitwise XOR value countSubsequences(A); } } // This code is contributed by splevel62. |
Javascript
<script> // Javascript program for the above approach // Function to count the subsequences // having odd bitwise XOR value function countSubsequences( A) { // Stores count of odd elements var odd = 0; // Stores count of even elements var even = 0; // Traverse the array A[] for ( var e1 = 0; e1<A.length; e1++) { // If el is odd if (A[e1] % 2 == 1) odd++; else even++; } // If count of odd elements is 0 if (odd == 0) document.write(0); else document.write((1 << (A.length - 1))); } // Driver Code // Given array A[] var A = [ 1, 3, 4 ]; // Function call to count subsequences // having odd bitwise XOR value countSubsequences(A); </script> |
4
Time Complexity: O(N)
Auxiliary Space: O(1)
Please Login to comment...