Given a binary string S, the task is to count the minimum number of jumps required to group all 1’s together.
Input: S = “000010011000100”
000010011000100 -> 000000111000100 requires 2 jumps.
000000111000100 -> 000000111100000 requires 3 jumps.
Hence, at least 5 jumps are required to group all 1’s together.
Input: S = “100010001”
100010001 -> 000110001 requires 3 jumps.
000110001 -> 000111000 requires 3 jumps.
We can observe that in order to minimize the number of jumps required for grouping all 1’s together, they need to be grouped near the median of their current positions. Calculate the median and the number of moves required to shift the 1’s to the nearest position of 0 in the left of the median. Perform the same operation for the right of the median.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(N)
- Minimum flips required to keep all 1s together in a Binary string
- Minimum adjacent swaps to group similar characters together
- Find the minimum of maximum length of a jump required to reach the last island in exactly k jumps
- Count minimum factor jumps required to reach the end of an Array
- Minimum given operations required to convert a given binary string to all 1's
- Check If every group of a's is followed by a group of b's of same length
- Number of permutations of a string in which all the occurrences of a given character occurs together
- Minimum operations required to convert a binary string to all 0s or all 1s
- Number of ways to make binary string of length N such that 0s always occur together in groups of size K
- Minimum flips required to form given binary string where every flip changes all bits to its right as well
- Given a sequence of words, print all anagrams together | Set 2
- Given a sequence of words, print all anagrams together using STL
- Given a sequence of words, print all anagrams together | Set 1
- Minimum flips required in a binary string such that all K-size substring contains 1
- Minimum non-adjacent pair flips required to remove all 0s from a Binary String
- Minimum number of adjacent swaps for arranging similar elements together
- Minimum number of operations required to obtain a given Binary String
- Minimum substring flips required to convert given binary string to another
- Minimum Group Flips to Make Binary Array Elements Same
- Check if all occurrences of a character appear together
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.