Count subsequences with same values of Bitwise AND, OR and XOR
Last Updated :
28 Nov, 2022
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 : 3
Explanation:
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 : 7
Explanation: All 7 non empty subsequences
have same values of AND, OR and XOR.
Input : a[] = [2, 2, 2, 3, 4]
Output : 6
Explanation: 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 2n - 1 subsequences contributed by these zeroes.
2) If there are n occurrences of a non-zero element x, then there will be 2n-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;
}
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
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
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
JavaScript
<script>
// function for finding count of possible subsequence
function countSubseq(arr, n)
{
let count = 0;
// creating a map to count the frequency of each element
let mp = new Map();
// store frequency of each element
for (let i = 0; i < n; i++){
mp[arr[i]]++;
if(mp.has(arr[i])){
mp.set(arr[i], mp.get(arr[i]) + 1)
}else{
mp.set(arr[i], 1)
}
}
// iterate through the map
for (let i of mp) {
// add all possible combination for key equal zero
if (i[0] == 0)
count += Math.pow(2, i[1]) - 1;
// add all (odd number of elements) possible
// combination for key other than zero
else
count += Math.pow(2, i[1] - 1);
}
return count;
}
// driver function
let arr = [ 2, 2, 2, 5, 6 ];
let n = arr.length;
document.write(countSubseq(arr, n));
// This code is contributed by _saurabh_jaiswal
</script>
Time complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
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
Number of subsets with same AND, OR and XOR values in an Array
Given an array arr[] of size N consisting of non-negative integers, the task is to find the number of non-empty subsets of the array such that the bitwise AND, bitwise OR and bitwise XOR values of the subsequence are equal to each other. Note: Since the answer can be large, mod it with 1000000007. E
14 min read
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: 4Explanation: Subsequences with odd Bitwise OR values are {1}, {2, 1}, {4, 1}, {2, 4, 1} Input: arr = [1,
5 min read
Count distinct possible Bitwise XOR values of subsets of an array
Given an array arr[] consisting of N integers, the task is to find the size of the set S such that Bitwise XOR of any subset of the array arr[] exists in the set S. Examples: Input: arr[] = {1, 2, 3, 4, 5}Output: 8Explanation:All possible Bitwise XOR values of subsets of the array arr[] are {0, 1, 2
14 min read
Count pairs with equal Bitwise AND and Bitwise OR value
Given an array, arr[] of size N, the task is to count the number of unordered pairs such that Bitwise AND and Bitwise OR of each pair is equal. Examples: Input: arr[] = {1, 2, 1} Output: 1 Explanation: Bitwise AND value and Bitwise OR value all possible pairs are: Bitwise AND of the pair(arr[0], arr
6 min read
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
Number of unique pairs whose bitwise XOR and OR are same
Given an integer N, the task is to find the number of pairs (say {a, b})from 1 to N (both inclusive) that satisfies the condition: a and b are distinct elements. The values of a | b and a ^ b are equal and a( a | b ) = b( a ^ b ) - ( a | b ) also holds true. Examples: Input: N=5Output:1Explanation:
9 min read
Count of pairs with bitwise XOR value greater than its bitwise AND value
Given an array arr that contains N positive Integers. Find the count of all possible pairs whose bitwise XOR value is greater than bitwise AND value Examples: Input : arr[]={ 12, 4, 15}Output: 2Explanation: 12 ^ 4 = 8, 12 & 4 = 4. so 12 ^ 4 > 12 & 4 4 ^ 15 = 11, 4 & 15 = 4. so 4 ^ 15
4 min read
Number of Subsequences with Even and Odd Sum | Set 2
Given an array arr[] of size N. The task is to find the number of subsequences whose sum is even and the number of subsequences whose sum is odd.Examples: Input: arr[] = {1, 2, 2, 3} Output: EvenSum = 7, OddSum = 8 There are 2N-1 possible subsequences. The subsequences with even sum are 1) {1, 3} Su
7 min read
Find all possible pairs with given Bitwise OR and Bitwise XOR values
Given two positive integers A and B representing Bitwise XOR and Bitwise OR of two positive integers, the task is to find all possible pairs (x, y) such that x ^ y is equal to A and x | y is equal to B. Examples: Input: A = 5, B = 7Output:2 73 66 37 2Explanation:7( XOR )2 = 5 and 7( OR )2 = 73( XOR
8 min read