Given a binary string str of size N and a positive integer K, the task is to find the minimum number of flips required to make all substring of size K contain at least one ‘1’.
Input: str = “0001”, K = 2
Flipping the bit at index 1 modifies str to “0101”.
All substrings of size 2 are “01”, “10”, and “01”.
Each substring contains at least one 1.
Input: str = “101”, K = 2
All substrings of size 2 are “10” and “01”.
Since both of them already have at least one ‘1’, no flips required in the original string.
Follow the steps below to solve the problem:
- The idea is use Sliding Window Technique to check whether every substring of length K contains any ‘1’ or not.
- Maintain a variable last_idx to store the last index of a window where the character was ‘1’. The value of this variable will be -1 if there is no ‘1’ present in the current window.
- For any such window, we will increment the number of flips by flipping the character at last index of the current window to ‘1’ and update the index last_idx to that index.
- Flipping the last character of the current window ensures that the following K-1 windows will have at least one ‘1’ as well. Thus, this approach minimizes the number of flips required.
- Repeat this process for the rest of the string and print the final count of flips required.
Below is the implementation of the above approach :
Time Complexity: O(N)
Auxiliary Space: O(1)
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.
- Minimum flips required to form given binary string where every flip changes all bits to its right as well
- Minimum flips required to keep all 1s together in a Binary string
- Minimum non-adjacent pair flips required to remove all 0s from a Binary String
- Minimize count of flips required such that no substring of 0s have length exceeding K
- Minimum Subarray flips required to convert all elements of a Binary Array to K
- Minimum Count of Bit flips required to make a Binary String Palindromic
- Minimize flips required to make all shortest paths from top-left to bottom-right of a binary matrix equal to S
- Minimum flips required to convert given string into concatenation of equal substrings of length K
- Minimum flips in a Binary array such that XOR of consecutive subarrays of size K have different parity
- Minimum flips required to maximize a number with k set bits
- Minimum flips required to generate continuous substrings of 0’s and 1’s
- Minimum number of flips with rotation to make binary string alternating
- Minimum K such that every substring of length atleast K contains a character c
- Minimum changes required to make first string substring of second string
- Minimum number of integers required such that each Segment contains at least one of them
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Minimum operations required to convert a binary string to all 0s or all 1s
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Minimum splits in a binary string such that every substring is a power of 4 or 6.
- Minimum Group Flips to Make Binary Array Elements Same
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.