Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Count of subsequences from a given Array having Binary Equivalence

  • Last Updated : 14 May, 2021

Given an array arr[] consisting of N integers, the task is to find the total number of distinct subsequences having Binary Equivalence.

A subsequence has Binary Equivalence if the sum of the count of set and unset bits in the binary representations of all the decimal numbers across the subsequence are equal.


Input: arr[] = {2, 7, 10}
Output: 0011
2 → 0010→1’s = 1, 0’s = 3
7 → 0111→1’s = 3, 0’s = 1
10 → 1010→1’s = 2, 0’s = 2
The subsequence [2, 7, 10] has Binary Equivalence because the number of 0’s and 1’s across the subsequence is 6 each.
Similarly, [2, 7] also has Binary Equivalence of 4 each. 
But [7, 10] does not have Binary Equivalence. 
Likewise, [10] has Binary Equivalence of 2 each. 
The total number of unique subsequences where Binary Equivalence is possible is 3. 
Since 10 is the largest element in the given array and the number of bits required to represent 10 in binary is 4. Hence, the number of bits present in the output needs to be 4.

Input: arr[] = {5, 7, 9, 12}
Output: 0111

Approach: The idea is to find the total number of bits required to represent the maximum element of the array.Follow these steps to solve this problem:

  1. Find the maximum element and the length of binary representation of the maximum element.
  2. Append 0 in the front other elements in binary representation, to make the number of bits in each element equal to the maximum number bits.
  3. Find all the subsequences of the given array.
  4. Find the total number of subsequences that have Binary Equivalence.
  5. Convert the total number into binary and append 0s if the length of the total number is less than the length of the maximum number to make both the lengths equal.

Below is the implementation of the above approach:


# Python program for the above approach
import itertools
# Function to find the number of
# subsequences having Binary Equivalence
def numberOfSubsequence(arr):
    # Find the maximum array element
    Max_element = max(arr)
    # Convert the maximum element
    # to its binary equivalent
    Max_Binary = "{0:b}".format(int(
    # Dictionary to store the count of
    # set and unset bits of all array elements
    Dic = {}
    for i in arr:
        Str = "{0:b}".format(int(i))
        if len(Str) <= len(Max_Binary):
            diff = len(Max_Binary)-len(Str)
            # Add the extra zeros before all
            # the elements which have length
            # smaller than the maximum element
            Str = ('0'*diff)+Str
        zeros = Str.count('0')
        ones = Str.count('1')
        # Fill the dictionary with number
        # of 0's and 1's
        Dic[int(i)] = [zeros, ones]
    all_combinations = []
    # Find all the combination
    for r in range(len(arr)+1):
        comb = itertools.combinations(arr, r)
        comlist = list(comb)
        all_combinations += comlist
    count = 0
    # Find all the combinations where
    # sum_of_zeros == sum_of_ones
    for i in all_combinations[1:]:
        sum0 = 0
        sum1 = 0
        for j in i:
            sum0 += Dic[j][0]
            sum1 += Dic[j][1]
        # Count the total combinations
        # where sum_of_zeros = sum_of_ones
        if sum0 == sum1:
            count += 1
    # Convert the count number to its
    # binary equivalent
    Str = "{0:b}".format(int(count))
    if len(Str) <= len(Max_Binary):
        diff = len(Max_Binary)-len(Str)
        # Append leading zeroes to
        # the answer if its length is
        # smaller than the maximum element
        Str = ('0'*diff) + Str
    # Print the result
# Driver Code
# Give array arr[]
arr = [5, 7, 9, 12]
# Function Call


Time Complexity: O(2N)
Auxiliary Space: O(N2)

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!