Count Subsequence of 101 in binary form of a number
Last Updated :
18 Apr, 2023
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));
Time Complexity: O(Log(N))
Auxiliary Space: O(Log(N))
Similar Reads
Maximum count of â010..â subsequences that can be removed from given Binary String Given a binary string S consisting of size N, the task is to find the maximum number of binary subsequences of the form "010.." of length at least 2 that can be removed from the given string S. Examples: Input: S = "110011010"Output: 3Explanation:Following are the subsequence removed:Operation 1: Ch
6 min read
Generate Binary String with equal number of 01 and 10 Subsequence Given an integer N (N > 2), the task is to generate a binary string of size N that consists of equal numbers of "10" & "01" subsequences and also the string should contain at least one '0' and one '1' Note: If multiple such strings exist, print any. Examples: Input: 4Output: 0110Explanation :
7 min read
Number of subsequences in a given binary string divisible by 2 Given binary string str of length N, the task is to find the count of subsequences of str which are divisible by 2. Leading zeros in a sub-sequence are allowed. Examples: Input: str = "101" Output: 2 "0" and "10" are the only subsequences which are divisible by 2.Input: str = "10010" Output: 22 Naiv
4 min read
Count number of binary strings of length N having only 0's and 1's Given an integer N, the task is to count the number of binary strings of length N having only 0's and 1's. Note: Since the count can be very large, return the answer modulo 10^9+7. Examples: Input: 2 Output: 4 Explanation: The numbers are 00, 01, 11, 10. Hence the count is 4. Input: 3 Output: 8 Expl
6 min read
Count of integers up to N which represent a Binary number Given an integer N, the task is to count every number i from 1 to N (both inclusive) such that i is a binary representation of some integer where N can be any value within the range[1, 109] Examples: Input: N = 100 Output: 4 Explanation: Valid integers are 1, 10, 11, 100 Input: N = 20 Output: 3 Expl
10 min read