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[0], second element be represented by s[1] and so on.

s = “100110111”

Suppose, we rotate the substring starting from s[2] and ending at s[4]. 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.

Examples:

Input : 100011001

k = 1

Output : 3

Explanation:

k is 1, hence you can rotate only once. Rotate the substring starting from s[1] and ending at s[5]. The resultant string will be : 111000001. Hence, maximum contiguous 1 are 3.

Input : 001100111000110011100

k = 2

Output : 8

Explanation:

k is 2, hence you can rotate twice. Rotate the substring starting at s[6] and ending at s[15]. Resultant string after first rotation : 001100001100011111100. Then, rotate the substring starting at s[8] and ending at s[12]. 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.

`// C++ program to calculate maximum contiguous ` `// ones in string ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to calculate maximum contiguous ones ` `int` `maxContiguousOnes(string s, ` `int` `k) ` `{ ` ` ` ` ` `int` `i, j, a, b, count; ` ` ` ` ` `// multiset is used to store frequency of ` ` ` `// 1's of each portion of contiguous 1 in ` ` ` `// string in decreasing order ` ` ` `multiset<` `int` `, greater<` `int` `> > m; ` ` ` ` ` `// this loop calculate all the frequency ` ` ` `// and stores them in multiset ` ` ` `for` `(i = 0; i < s.length(); i++) { ` ` ` `if` `(s[i] == ` `'1'` `) { ` ` ` `count = 0; ` ` ` `j = i; ` ` ` `while` `(s[j] == ` `'1'` `&& j < s.length()) { ` ` ` `count++; ` ` ` `j++; ` ` ` `} ` ` ` `m.insert(count); ` ` ` `i = j - 1; ` ` ` `} ` ` ` `} ` ` ` ` ` `// if their is no 1 in string, then return 0 ` ` ` `if` `(m.size() == 0) ` ` ` `return` `0; ` ` ` ` ` `// calculates maximum contiguous 1's on ` ` ` `// doing rotations ` ` ` `while` `(k > 0 && m.size() != 1) { ` ` ` ` ` `// Delete largest two elements ` ` ` `a = *(m.begin()); ` ` ` `m.erase(m.begin()); ` ` ` `b = *(m.begin()); ` ` ` `m.erase(m.begin()); ` ` ` ` ` `// insert their sum back into the multiset ` ` ` `m.insert(a + b); ` ` ` `k--; ` ` ` `} ` ` ` ` ` `// return maximum contiguous ones ` ` ` `// possible after k rotations ` ` ` `return` `*(m.begin()); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string s = ` `"10011110011"` `; ` ` ` `int` `k = 1; ` ` ` `cout << maxContiguousOnes(s, k); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

6

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.

## Recommended Posts:

- Rotations of a Binary String with Odd Value
- Find element at given index after a number of rotations
- Number of strings which starts and ends with same character after rotations
- Find the Mth element of the Array after K left rotations
- Mth element after K Right Rotations of an Array
- Print all possible rotations of a given Array
- Find maximum value of Sum( i*arr[i]) with only rotations on given array allowed
- Maximum sum of i*arr[i] among all rotations of a given array
- Generate all rotations of a given string
- Minimum rotations required to get the same string
- Minimum rotations required to get the same String | Set-2
- Minimum circular rotations to obtain a given numeric string by avoiding a set of given strings
- Find the maximum possible Binary Number from given string
- Queries to find maximum sum contiguous subarrays of given length in a rotating array
- Print string after removing all (“10” or “01”) from the binary string
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9 | Set 2
- Distinct state codes that appear in a string as contiguous sub-strings
- Contiguous subsegments of a string having distinct subsequent characters
- Longest palindromic string possible after removal of a substring

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.