Count subsequences having odd Bitwise OR values in an array
Last Updated :
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: 4
Explanation: 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: O(N*2^N)
Auxiliary Space: O(1)
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 i
- 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++
// C++ implementation for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to count the subsequences
// having odd bitwise OR value
int 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 Code
int main()
{
// Given array arr[]
vector<int> arr = {2, 4, 1};
cout << countSubsequences(arr);
}
Java
// Java implementation for the above approach
import 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
# Python3 implementation for the above approach
# Function to count the subsequences
# having odd bitwise OR value
def 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 Code
if __name__ == "__main__" :
# Given array arr[]
arr = [2, 4, 1];
print(countSubsequences(arr));
# This code is contributed by AnkThon
C#
// Java implementation for the above approach
using 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
JavaScript
<script>
// JavaScript Program to implement
// the above approach
// Function to count the subsequences
// having odd bitwise OR value
function 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>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
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: 3Explanation: The subsequences of the given array having odd Bitwise AND values are {3} = 3, {1} = 1, {3, 1} = 3
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 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
Count Subsequences with ordered integers in Array Given an array nums[] of N positive integers, the task is to find the number of subsequences that can be created from the array where each subsequence contains all integers from 1 to its size in any order. If two subsequences have different chosen indices, then they are considered different. Example
7 min read
Count subarrays having odd Bitwise XOR Given an array arr[] of size N, the task is to count the number of subarrays from the given array having odd Bitwise XOR value. Examples: Input: arr[] = {1, 4, 7, 9, 10}Output: 8Explanation: The subarrays having odd Bitwise XOR are {1}, {1, 4}, {1, 4, 7, 9}, {1, 4, 7, 9, 10}, {7}, {9}, {4, 7}, {9, 1
11 min read
Count of possible subarrays and subsequences using given length of Array Given an integer N which denotes the length of an array, the task is to count the number of subarray and subsequence possible with the given length of the array.Examples: Input: N = 5 Output: Count of subarray = 15 Count of subsequence = 32Input: N = 3 Output: Count of subarray = 6 Count of subseque
3 min read