Count of substrings whose Decimal equivalent is greater than or equal to K

Given an integer K and a binary string S of length N, the task is to find out the number of substrings whose decimal equivalent is greater than or equal to K.

Examples:

Input: K = 3, S = “11100”
Output: 8
Explanation:
There are 8 such substring whose decimal equivalent is greater than or equal to 3, as mentioned below:
Substring – Decimal Equivalent
“100” – 4,
“1100” – 12,
“11100” – 28,
“110” – 6,
“1110” – 14,
“11” – 3,
“111” – 7,
“11” – 3

Input: K = 5, S = “10110110”
Output: 19
Explanation:
There are 19 such substring whose decimal equivalent is greater than or equal to 5

Naive Approach: Find out all the substrings and for each substring, convert it from binary to decimal and check if it is greater than or equal to K. Count the number of every such substring found.



Efficient Approach: Using Two-Pointer technique

  1. The idea is to maintain two-pointers L and R.
  2. Fix the position of the right pointer ‘R’ of the substring to length – 1 and iterate with a loop until the value of R is positive:
    • Intialize the value of L to R, for considering the substring of length 1
    • Decrement the value of L by 1 until the decimal equivalent of the substring of length R – L + 1 is greater than or equal to K
    • Increment the counter by the number of bits in the left of the L.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to count the
// substrings whose decimal equivalent
// is greater than or equal to K
#include <bits/stdc++.h>
using namespace std;
  
// Function to count number of
// substring whose decimal equivalent
// is greater than or equal to K
unsigned long long countSubstr(
    string& s, int k)
{
  
    int n = s.length();
  
    // Left pointer of the substring
    int l = n - 1;
  
    // Right pointer of the substring
    int r = n - 1;
    int arr[n];
    int last_indexof1 = -1;
  
    // Loop to maintain the last
    // occurrence of the 1 in the string
    for (int i = 0; i < n; i++) {
        if (s[i] == '1') {
            arr[i] = i;
            last_indexof1 = i;
        }
        else {
            arr[i] = last_indexof1;
        }
    }
  
    // Variable to count the substring
    unsigned long long no_of_substr = 0;
  
    // Loop to maintain the every
    // possible end index of the substring
    for (r = n - 1; r >= 0; r--) {
        l = r;
  
        // Loop to find the substring
        // whose decimal equivalent is
        // greater than or equal to K
        while (l >= 0
               && (r - l + 1) <= 64
               && stoull(
                      s.substr(l, r - l + 1), 0, 2)
                      < k) {
            l--;
        }
  
        // Condition to check no
        // of bits is out of bound
        if (r - l + 1 <= 64)
            no_of_substr += l + 1;
        else {
            no_of_substr += arr[l + 1] + 1;
        }
    }
    return no_of_substr;
}
  
// Driver Code
int main()
{
    string s = "11100";
    unsigned long long int k = 3;
    cout << countSubstr(s, k);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to count the
// subStrings whose decimal equivalent
// is greater than or equal to K
import java.util.*;
  
class GFG{
   
// Function to count number of
// subString whose decimal equivalent
// is greater than or equal to K
static long countSubstr(
    String s, int k)
{
   
    int n = s.length();
   
    // Left pointer of the subString
    int l = n - 1;
   
    // Right pointer of the subString
    int r = n - 1;
    int []arr = new int[n];
    int last_indexof1 = -1;
   
    // Loop to maintain the last
    // occurrence of the 1 in the String
    for (int i = 0; i < n; i++) {
        if (s.charAt(i) == '1') {
            arr[i] = i;
            last_indexof1 = i;
        }
        else {
            arr[i] = last_indexof1;
        }
    }
   
    // Variable to count the subString
    long no_of_substr = 0;
   
    // Loop to maintain the every
    // possible end index of the subString
    for (r = n - 1; r >= 0; r--) {
        l = r;
   
        // Loop to find the subString
        // whose decimal equivalent is
        // greater than or equal to K
        while (l >= 0
               && (r - l + 1) <= 64
               && Integer.valueOf(s.substring(l, r + 1),2)
                      < k) {
            l--;
        }
   
        // Condition to check no
        // of bits is out of bound
        if (r - l + 1 <= 64)
            no_of_substr += l + 1;
        else {
            no_of_substr += arr[l + 1] + 1;
        }
    }
    return no_of_substr;
}
   
// Driver Code
public static void main(String[] args)
{
    String s = "11100";
    int k = 3;
    System.out.println(countSubstr(s, k));
}
}
  
// This code is contributed by PrinciRaj1992

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation to count the
# substrings whose decimal equivalent
# is greater than or equal to K
  
# Function to count number of
# substring whose decimal equivalent
# is greater than or equal to K
def countSubstr(s, k):
  
    n = len(s)
  
    # Left pointer of the substring
    l = n - 1
  
    # Right pointer of the substring
    r = n - 1
    arr = [0]*n
    last_indexof1 = -1
  
    # Loop to maintain the last
    # occurrence of the 1 in the string
    for i in range(n):
        if (s[i] == '1'):
            arr[i] = i
            last_indexof1 = i
  
        else:
            arr[i] = last_indexof1
  
    # Variable to count the substring
    no_of_substr = 0
  
    # Loop to maintain the every
    # possible end index of the substring
    for r in range(n - 1, -1, -1):
        l = r
  
        # Loop to find the substring
        # whose decimal equivalent is
        # greater than or equal to K
        while (l >= 0 and (r - l + 1) <= 64 and int(s[l:r + 1], 2)< k):
            l -= 1
  
        # Condition to check no
        # of bits is out of bound
        if (r - l + 1 <= 64):
            no_of_substr += l + 1
        else:
            no_of_substr += arr[l + 1] + 1
  
    return no_of_substr
  
# Driver Code
  
s = "11100"
k = 3
print(countSubstr(s, k))
  
# This code is contributed by mohit kumar 29

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to count the
// subStrings whose decimal equivalent
// is greater than or equal to K
using System;
  
class GFG{
    
// Function to count number of
// subString whose decimal equivalent
// is greater than or equal to K
static long countSubstr(
    String s, int k)
{
    
    int n = s.Length;
    
    // Left pointer of the subString
    int l = n - 1;
    
    // Right pointer of the subString
    int r = n - 1;
    int []arr = new int[n];
    int last_indexof1 = -1;
    
    // Loop to maintain the last
    // occurrence of the 1 in the String
    for (int i = 0; i < n; i++) {
        if (s[i] == '1') {
            arr[i] = i;
            last_indexof1 = i;
        }
        else {
            arr[i] = last_indexof1;
        }
    }
    
    // Variable to count the subString
    long no_of_substr = 0;
    
    // Loop to maintain the every
    // possible end index of the subString
    for (r = n - 1; r >= 0; r--) {
        l = r;
    
        // Loop to find the subString
        // whose decimal equivalent is
        // greater than or equal to K
        while (l >= 0
               && (r - l + 1) <= 64 && (int)(Convert.ToInt32(s.Substring(l, r + 1-l), 2))
                      < k) {
            l--;
        }
    
        // Condition to check no
        // of bits is out of bound
        if (r - l + 1 <= 64)
            no_of_substr += l + 1;
        else {
            no_of_substr += arr[l + 1] + 1;
        }
    }
    return no_of_substr;
}
    
// Driver Code
public static void Main(String[] args)
{
    String s = "11100";
    int k = 3;
    Console.WriteLine(countSubstr(s, k));
}
}
  
// This code is contributed by Rajput-Ji

chevron_right


Output:

8

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.