# 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 single element subsequence, we consider the element itself as result of XOR, AND and OR. Therefore all single element subsequences are always counted as part of result.

Examples:

Input :a = [1, 3, 7]Output :3Explanation:There are 7 non empty subsequence . subsequence OR AND XOR {1} 1 1 1 {3} 3 3 3 {7} 7 7 7 {1, 3} 3 1 2 {1, 7} 7 1 6 {3, 7} 7 3 4 {1, 3, 7} 7 1 5 Out of 7, there are 3 subsequences {1} {3} {7} which have same values of AND, OR and XOR.Input :a[] = [0, 0, 0]Output :7Explanation:All 7 non empty subsequences have same values of AND, OR and XOR.Input :a[] = [2, 2, 2, 3, 4]Output :6Explanation:subsequence {2}, {2}, {2}, {2, 2, 2}, {3}, {4} have same values of AND, OR and XOR.

1) If there are n occurrences of zeroes in the given array, then will be 2^{n} – 1 subsequences contributed by these zeroes.

2) If there are n occurrences of a non-zero element x, then there will be 2^{n-1} subsequences contributed by occurrences of this element. Please note that, in case of non-zero elements, only odd number of occurrences can cause same results for bitwise operators.

Find count of each element in the array then apply the above formulas.

## C++

`#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function for finding count of possible subsequence ` `int` `countSubseq(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `int` `count = 0; ` ` ` ` ` `// creating a map to count the frequency of each element ` ` ` `unordered_map<` `int` `, ` `int` `> mp; ` ` ` ` ` `// store frequency of each element ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `mp[arr[i]]++; ` ` ` ` ` `// iterate through the map ` ` ` `for` `(` `auto` `i : mp) { ` ` ` ` ` `// add all possible combination for key equal zero ` ` ` `if` `(i.first == 0) ` ` ` `count += ` `pow` `(2, i.second) - 1; ` ` ` ` ` `// add all (odd number of elements) possible ` ` ` `// combination for key other than zero ` ` ` `else` ` ` `count += ` `pow` `(2, i.second - 1); ` ` ` `} ` ` ` `return` `count; ` `} ` ` ` `// driver function ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 2, 2, 2, 5, 6 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `cout << countSubseq(arr, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`import` `java .io.*; ` `import` `java.util.*; ` ` ` ` ` `class` `GFG { ` ` ` `// function for finding count of possible subsequence ` `static` `int` `countSubseq(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `int` `count = ` `0` `; ` ` ` ` ` `// creating a map to count the frequency of each element ` ` ` `HashMap<Integer,Integer>mp=` `new` `HashMap<Integer,Integer>(); ` ` ` ` ` `// store frequency of each element ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `if` `(mp.containsKey(arr[i])) ` ` ` `mp.put(arr[i],mp.get(arr[i])+` `1` `); ` ` ` `else` ` ` `mp.put(arr[i],` `1` `); ` ` ` ` ` `// iterate through the map ` ` ` `for` `(Map.Entry<Integer,Integer>entry:mp.entrySet()) { ` ` ` ` ` `// add all possible combination for key equal zero ` ` ` `if` `(entry.getKey() == ` `0` `) ` ` ` `count += Math.pow(` `2` `, entry.getValue()) - ` `1` `; ` ` ` ` ` `// add all (odd number of elements) possible ` ` ` `// combination for key other than zero ` ` ` `else` ` ` `count += Math.pow(` `2` `, entry.getValue()- ` `1` `); ` ` ` `} ` ` ` `return` `count; ` `} ` ` ` `// driver function ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `arr[] = { ` `2` `, ` `2` `, ` `2` `, ` `5` `, ` `6` `}; ` ` ` `int` `n=arr.length; ` ` ` `System.out.println(countSubseq(arr, n)); ` `} ` `} ` ` ` `// This code is contributed by apurva raj ` |

*chevron_right*

*filter_none*

## C#

`using` `System; ` `using` `System.Collections.Generic; ` `class` `GFG{ ` ` ` `// function for finding count of possible subsequence ` `static` `int` `countSubseq(` `int` `[]arr, ` `int` `n) ` `{ ` ` ` `int` `count = 0; ` ` ` ` ` `// creating a map to count the frequency of each element ` ` ` `Dictionary<` `int` `, ` `int` `> mp = ` `new` `Dictionary<` `int` `,` `int` `>(); ` ` ` ` ` `// store frequency of each element ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `if` `(mp.ContainsKey(arr[i])) ` ` ` `{ ` ` ` `var` `val = mp[arr[i]]; ` ` ` `mp.Remove(arr[i]); ` ` ` `mp.Add(arr[i], val + 1); ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `mp.Add(arr[i], 1); ` ` ` `} ` ` ` `} ` ` ` ` ` `// iterate through the map ` ` ` `foreach` `(KeyValuePair<` `int` `, ` `int` `> entry ` `in` `mp) { ` ` ` ` ` `// add all possible combination for key equal zero ` ` ` `if` `(entry.Key == 0) ` ` ` `count += (` `int` `)(Math.Pow(2, entry.Value - 1)); ` ` ` ` ` `// add all (odd number of elements) possible ` ` ` `// combination for key other than zero ` ` ` `else` ` ` `count += (` `int` `)(Math.Pow(2, entry.Value - 1)); ` ` ` `} ` ` ` `return` `count; ` `} ` ` ` `// Driver function ` `public` `static` `void` `Main(String []args) ` ` ` `{ ` ` ` `int` `[]arr = { 2, 2, 2, 5, 6 }; ` ` ` `int` `n = arr.Length; ` ` ` `Console.WriteLine(countSubseq(arr, n)); ` `} ` `} ` ` ` `// This code is contributed by shivanisinghss2110 ` |

*chevron_right*

*filter_none*

## Python3

`# function for finding count of possible subsequence ` `def` `countSubseq(arr, n): ` ` ` `count ` `=` `0` ` ` ` ` `# creating a map to count the frequency of each element ` ` ` `mp ` `=` `{} ` ` ` ` ` `# store frequency of each element ` ` ` `for` `x ` `in` `arr: ` ` ` `if` `x ` `in` `mp.keys(): ` ` ` `mp[x]` `+` `=` `1` ` ` `else` `: ` ` ` `mp[x]` `=` `1` ` ` ` ` `# iterate through the map ` ` ` `for` `i ` `in` `mp.keys(): ` ` ` ` ` `# add all possible combination for key equal zero ` ` ` `if` `(i ` `=` `=` `0` `): ` ` ` `count ` `+` `=` `pow` `(` `2` `, mp[i]) ` `-` `1` ` ` ` ` `# add all (odd number of elements) possible ` ` ` `# combination for key other than zero ` ` ` `else` `: ` ` ` `count ` `+` `=` `pow` `(` `2` `, mp[i] ` `-` `1` `) ` ` ` `return` `count ` ` ` `# Driver function ` `arr` `=` `[` `2` `, ` `2` `, ` `2` `, ` `5` `, ` `6` `] ` `n ` `=` `len` `(arr) ` `print` `(countSubseq(arr, n)) ` ` ` `# This code is contributed by apurva raj ` |

*chevron_right*

*filter_none*

**Output:**

6

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Find subsequences with maximum Bitwise AND and Bitwise OR
- Count all increasing subsequences
- Count Distinct Subsequences
- Count of 'GFG' Subsequences in the given string
- Count all subsequences having product less than K
- Count of subsequences in an array with sum less than or equal to X
- Count of subsequences of length 4 in form (x, x, x+1, x+1) | Set 2
- Count of subsequences which consists exactly K prime numbers
- Count of possible subarrays and subsequences using given length of Array
- Count of subsequences having maximum distinct elements
- Count of all subsequences having adjacent elements with different parity
- Find the count of subsequences where each element is divisible by K
- Count the number of subsequences of length k having equal LCM and HCF
- Count of subsequences whose product is a difference of square of two integers
- Count of subsequences of length atmost K containing distinct prime elements
- Count minimum number of subsets (or subsequences) with consecutive numbers
- Count the number of contiguous increasing and decreasing subsequences in a sequence
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Count of pairs whose bitwise AND is a power of 2
- Count pairs with Bitwise XOR as ODD number

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.