Open In App

Count Subsequence of 101 in binary form of a number

Last Updated : 18 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a number N, the task is to count the occurrences of 101 subsequences in the binary representation of that number.

Examples:

Input: N = 10
Output: 1
Explanation: The binary representation of the number 10 is 1010 and there is only one subsequence 101 which is starting from the left end of binary form is present.

Input: N = 21
Output: 4
Explanation: The binary representation of 21 is 10101 and there are total 4 subsequences of 101 are present.

Approach: To solve the problem follow the below observations:

Observations:

The key to this problem is observations. So if we have binary representation like 101, then the total contribution of a zero is multiplication of the number of 1’s present at its left and number of 1’s present at its right.

Illustration:

For example, 21 whose binary is 10101 if we talk about 0 present at index-1 there is one 1 at its left and two 1 at right so it can contribute total 1*2=2, and for another 0 present at index-3 there is two 1 at left and one 1 at right so it will contribute 2*1=2. So the answer will be 2+2 = 4.

Steps to implement the above approach:

  • Call the totalCount function to return the count of 101 subsequences.
  • Iterate over each bit in n
    • First, for every 0 bit, we push the number of 1s to its right in the right vector. Also, simultaneously increase the count of the number of 1 bit.
  • Iterate over this vector, and update the ans with the multiplication of the number of 1s on left and right respectively for every 0.

Below is the code to implement the above steps:

C++

#include <bits/stdc++.h>
using namespace std;
long long TotalCount(long long n)
{

    // To keep number of 1 at right
    vector<int> right;

    // Total answer
    long long ans = 0;

    // Total 1
    int one = 0;

    while (n != 0) {

        // If bit is set
        if (n & 1) {
            one++;
        }

        // Else the total number of one
        // till now is right for current 0
        else {
            right.push_back(one);
        }

        n >>= 1;
    }

    for (auto x : right) {

        // Number of one's at left =
        // total-right
        int left = one - x;

        // Adding to answer the
        // contribution of current 0
        ans += (left * x);
    }

    return ans;
}

// Drivers code
int main()
{

    // Given number
    long long n = 21;

    // Calling function
    cout << TotalCount(n) << endl;

    return 0;
}

Java

import java.util.*;

public class Main {
    public static long TotalCount(long n)
    {

        // To keep number of 1 at right
        List<Integer> right = new ArrayList<>();

        // Total answer
        long ans = 0;

        // Total 1
        int one = 0;

        while (n != 0) {

            // If bit is set
            if ((n & 1) == 1) {
                one++;
            }

            // Else the total number of one
            // till now is right for current 0
            else {
                right.add(one);
            }

            n >>= 1;
        }

        for (int x : right) {

            // Number of one's at left =
            // total-right
            int left = one - x;

            // Adding to answer the
            // contribution of current 0
            ans += (left * x);
        }

        return ans;
    }

    // Drivers code
    public static void main(String[] args)
    {

        // Given number
        long n = 21;

        // Calling function
        System.out.println(TotalCount(n));
    }
}
// This code is contributed by Prajwal Kandekar

Python3

def total_count(n):
    # To keep number of 1 at right
    right = []

    # Total answer
    ans = 0

    # Total 1
    one = 0

    while n != 0:
        # If bit is set
        if n & 1:
            one += 1
        # Else the total number of one
        # till now is right for current 0
        else:
            right.append(one)
        n >>= 1

    for x in right:
        # Number of one's at left =
        # total-right
        left = one - x

        # Adding to answer the
        # contribution of current 0
        ans += left * x

    return ans

# Driver code
if __name__ == "__main__":
    # Given number
    n = 21

    # Calling function
    print(total_count(n))

C#

using System;
using System.Collections.Generic;

public class GFG{
       public static long TotalCount(long n)
    {

        // To keep number of 1 at right
        List<int> right = new List<int>();

        // Total answer
        long ans = 0;

        // Total 1
        int one = 0;

        while (n != 0) {

            // If bit is set
            if ((n & 1) == 1) {
                one++;
            }

            // Else the total number of one
            // till now is right for current 0
            else {
                right.Add(one);
            }

            n >>= 1;
        }

        foreach (int x in right) {

            // Number of one's at left =
            // total-right
            int left = one - x;

            // Adding to answer the
            // contribution of current 0
            ans += (left * x);
        }

        return ans;
    }

    // Drivers code
    public static void Main(String[] args)
    {

        // Given number
        long n = 21;

        // Calling function
        Console.WriteLine(TotalCount(n));
    }
}

Javascript

function TotalCount(n) {
//To keep number of 1 at right
let right = [];
//Total answer
let ans = 0;
//To keep number of 1 at right
let one = 0;

while (n != 0) {
    if (n & 1) {
        one++;
    }
    else {
        right.push(one);
    }

    n >>= 1;
}

for (let x of right) {
    let left = one - x;
    ans += (left * x);
}

return ans;
}

// Given number
let n = 21;
// Calling function
console.log(TotalCount(n));
Output

4

Time Complexity: O(Log(N))
Auxiliary Space: O(Log(N))


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads