Given a binary string S, and an integer K, the task is to find the minimum number of flips required such that every substring of length K contains at least one ‘1’.
Input: S = “10000001” K = 2
We need only 3 changes in string S ( at position 2, 4 and 6 ) so that the string contain at least one ‘1’ in every sub-string of length 2.
Input: S = “000000” K = 3
We need only 3 changes in string S ( at position 2 and 5 ) so that the string contain at least one ‘1’ in every sub-string of length 3.
Input: S = “111010111” K = 2
To solve the problem, the simplest approach is to iterate for each substring of length K and find the minimum number of flips required to satisfy the given condition.
Time complexity: O(N * K)
The idea is to use Sliding Window Approach.
- Set a window size of K.
- Store the index of previous appearance of 1.
- If the current bit is unset and the difference between the current ith bit and the previous set bit exceeds K, set the current bit and store the current index as that of the previous set bit and proceed further.
Below is the implementation of the above approach:
Time complexity: O(N + K)
Auxiliary Space: O(1)
- Minimum flips required to form given binary string where every flip changes all bits to its right as well
- Find Bit whose minimum sequence flips makes all bits same
- 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
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Check if bits of a number has count of consecutive set bits in increasing order
- Minimum Count of Bit flips required to make a Binary String Palindromic
- Minimum flips required in a binary string such that all K-size substring contains 1
- Check if all bits can be made same by flipping two consecutive bits
- For every set bit of a number toggle bits of other
- Longest Subsequence with at least one common digit in every element
- Longest subsequence with at least one character appearing in every string
- Count of sub-strings that contain character X at least once
- Sub-strings that start and end with one character and have at least one other
- Count of N size strings consisting of at least one vowel and one consonant
- Count minimum right flips to set all values in an array
- Minimum flips to make all 1s in left and 0s in right | Set 2
- Minimum flips to make all 1s in left and 0s in right | Set 1 (Using Bitmask)
- Lexicographically largest subsequence such that every character occurs at least k times
- Find K for every Array element such that at least K prefixes are ≥ 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.