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> |
6
Â
Time complexity: O(N)
Auxiliary Space: O(N)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!