# 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:4Explaination: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**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
- 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>` |

**Output**

4

**Time Complexity: ****Auxiliary Space: **

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.