Input: K = 3, S = “11100”
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”
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
- The idea is to maintain two-pointers L and R.
- 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:
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.
- Count number of substrings with numeric value greater than X
- Count Substrings with equal number of 0s, 1s and 2s
- Maximize the decimal equivalent by flipping only a contiguous set of 0s
- Minimise N such that sum of count of all factors upto N is greater than or equal to X
- Noble integers in an array (count of greater elements is equal to value)
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Number of substrings with odd decimal value in a binary string
- Convert all substrings of length 'k' from base 'b' to decimal
- Counting even decimal value substrings in a binary string
- Count elements such that there are exactly X elements with values greater than or equal to X
- Count of Array elements greater than or equal to twice the Median of K trailing Array elements
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Split the binary string into substrings with equal number of 0s and 1s
- Split a given string into substrings of length K with equal sum of ASCII values
- Minimum flips required to convert given string into concatenation of equal substrings of length K
- Count all substrings having character K
- Count substrings that contain all vowels | SET 2
- Count of substrings containing only the given character
- Count substrings with same first and last characters
- Longest Subarray with Sum greater than Equal to Zero
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.