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:
- Count number of substrings with numeric value greater than X
- Count Substrings with equal number of 0s, 1s and 2s
- 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
- Counting even decimal value substrings in a binary string
- Convert all substrings of length 'k' from base 'b' to decimal
- Number of substrings with odd decimal value in a binary string
- Count elements such that there are exactly X elements with values greater than or equal to X
- Split the binary string into substrings with equal number of 0s and 1s
- Count substrings that contain all vowels | SET 2
- Count substrings with same first and last characters
- Count all substrings having character K
- Count of substrings which contains a given character K times
- Count of substrings of a binary string containing K ones
- Number of substrings with count of each character as k
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.