Given a binary string, you can rotate any substring of this string. For Example, let string be denoted by s. Let the first element of string be represented by s, second element be represented by s and so on.
s = “100110111”
Suppose, we rotate the substring starting from s and ending at s. Then the string after this operation will be:
Resultant String = “101100111”
Now, you are allowed to do at most k operations to rotate any substring. You have to tell the maximum number of contiguous 1 you can make in this string in k or less than k rotations of substring.
Input : 100011001
k = 1
Output : 3
k is 1, hence you can rotate only once. Rotate the substring starting from s and ending at s. The resultant string will be : 111000001. Hence, maximum contiguous 1 are 3.
Input : 001100111000110011100
k = 2
Output : 8
k is 2, hence you can rotate twice. Rotate the substring starting at s and ending at s. Resultant string after first rotation : 001100001100011111100. Then, rotate the substring starting at s and ending at s. Resultant string after second rotation : 001100000001111111100. Hence, maximum number of contiguous 1 are 8.
Concept For Solving:
In order to solve this problem, we will maintain the frequency of 1’s in portion of contiguous 1’s in the original string in a multiset. Then on each rotation, we will rotate that substring such that, 2 portions of contiguous 1(with maximum frequency) in the string come together. We will do this, by sorting the multiset from greatest to smallest element. We will take out top 2 elements of multiset and insert their sum back into the multiset. We will continue to do this until k rotations are completed or number of elements in multiset is reduced to 1.
- Rotations of a Binary String with Odd Value
- Maximum sum of i*arr[i] among all rotations of a given array
- Maximum difference of zeros and ones in binary string
- Find maximum value of Sum( i*arr[i]) with only rotations on given array allowed
- Arrange a binary string to get maximum value within a range of indices
- Maximum difference of zeros and ones in binary string | Set 2 (O(n) time)
- Maximum Consecutive Zeroes in Concatenated Binary String
- Maximum length of consecutive 1's in a binary string in Python using Map function
- Generate all rotations of a given string
- Minimum rotations required to get the same string
- Distinct state codes that appear in a string as contiguous sub-strings
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9 | Set 2
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9
- Python | Check if a given string is binary string or not
- Remove one bit from a binary number to get maximum value
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.