Given a binary string s and a number K, the task is to find the maximum number of 0s that can be replaced by 1s such that two adjacent 1s are separated by at least K 0s in between them.
Input: K = 2, s = “000000”
Change the 0s at position 0 and 3. Then the final string will be “100100” such that every 1 is separated by at least 2 0s.
Input: K = 1, s = “01001000100000”
Change the 0s at position 6, 10, and 12. Then the final string will be “01001010101010” such that every 1 is separated by at least 1 0s.
- Insert the all the characters of the given string in an array(say arr).
- Traverse the array arr and convert all the 0s to -1 which are located at <= K places near an already existing 1. This operation gives all the possible positions of the string where 1 can be inserted.
- Initialize a count variable to 0.
- Traverse the array arr from the left till the end. As soon as the first 0 is encountered replace it with 1 and increase the count value.
- Convert all the 0s to -1 which are located at <= K places near the newly converted 1.
- Keep traversing the array till the end and every time a 0 is encountered, repeat the steps 4 – 5.
Below is the implementation of the above approach:
Time Complexity: O(N * K)
Auxillary Space: O(1)
- Maximum number of 0s that can be flipped such that Array has no adjacent 1s
- Count number of bits to be flipped to convert A to B | Set-2
- Count number of bits to be flipped to convert A to B
- Count of total bits toggled/flipped in binary representation of 0 to N
- Minimize the cost to make all the adjacent elements distinct in an Array
- Minimum replacements in a string to make adjacent characters unequal
- Minimum replacements to make adjacent characters unequal in a ternary string
- Minimum elements to be inserted in Array to make adjacent differences equal
- Minimum replacements to make adjacent characters unequal in a ternary string | Set-2
- Count of subsets not containing adjacent elements
- Count of arrays in which all adjacent elements are such that one of them divide the another
- Count of all subsequences having adjacent elements with different parity
- Count of adjacent Vowel Consonant Pairs
- Minimize the count of adjacent pairs with different parity
- Count of strings where adjacent characters are of difference one
- Count of subsets of integers from 1 to N having no adjacent elements
- Count possible combinations of pairs with adjacent elements from first N numbers
- Count of numbers upto N having absolute difference of at most K between any two adjacent digits
- Count binary strings with k times appearing adjacent two set bits
- Count non-adjacent subsets from numbers arranged in Circular fashion
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.