C++ Program to Find Maximum number of 0s placed consecutively at the start and end in any rotation of a Binary String Last Updated : 27 Jan, 2022 Comments Improve Suggest changes Like Article Like Report Given a binary string S of size N, the task is to maximize the sum of the count of consecutive 0s present at the start and end of any of the rotations of the given string S. Examples: Input: S = "1001"Output: 2Explanation:All possible rotations of the string are:"1001": Count of 0s at the start = 0; at the end = 0. Sum= 0 + 0 = 0."0011": Count of 0s at the start = 2; at the end = 0. Sum = 2 + 0=2"0110": Count of 0s at the start = 1; at the end = 1. Sum= 1 + 1 = 2."1100": Count of 0s at the start = 0; at the end = 2. Sum = 0 + 2 = 2Therefore, the maximum sum possible is 2. Input: S = "01010"Output: 2Explanation: All possible rotations of the string are:"01010": Count of 0s at the start = 1; at the end = 1. Sum= 1+1=1"10100": Count of 0s at the start = 0; at the end = 2. Sum= 0+2=2"01001": Count of 0s at the start = 1; at the end = 0. Sum= 1+0=1"10010": Count of 0s at the start = 0; at the end = 1. Sum= 0+1=1"00101": Count of 0s at the start = 2; at the end = 0. Sum= 2+0=2Therefore, the maximum sum possible is 2. Naive Approach: The simplest idea is to generate all rotations of the given string and for each rotation, count the number of 0s present at the beginning and end of the string and calculate their sum. Finally, print the maximum sum obtained. Below is the implementation of the above approach: C++ // C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the maximum sum of // consecutive 0s present at the start // and end of a string present in any // of the rotations of the given string void findMaximumZeros(string str, int n) { // Check if all the characters // in the string are 0 int c0 = 0; // Iterate over characters // of the string for (int i = 0; i < n; ++i) { if (str[i] == '0') c0++; } // If the frequency of '1' is 0 if (c0 == n) { // Print n as the result cout << n; return; } // Concatenate the string // with itself string s = str + str; // Stores the required result int mx = 0; // Generate all rotations of the string for (int i = 0; i < n; ++i) { // Store the number of consecutive 0s // at the start and end of the string int cs = 0; int ce = 0; // Count 0s present at the start for (int j = i; j < i + n; ++j) { if (s[j] == '0') cs++; else break; } // Count 0s present at the end for (int j = i + n - 1; j >= i; --j) { if (s[j] == '0') ce++; else break; } // Calculate the sum int val = cs + ce; // Update the overall // maximum sum mx = max(val, mx); } // Print the result cout << mx; } // Driver Code int main() { // Given string string s = "1001"; // Store the size of the string int n = s.size(); findMaximumZeros(s, n); return 0; } Output: 2 Time Complexity: O(N2)Auxiliary Space: O(N) Efficient Approach: The idea is to find the maximum number of consecutive 0s in the given string. Also, find the sum of consecutive 0s at the start and the end of the string, and then print the maximum out of them. Follow the steps below to solve the problem: Check if the frequency of '1' in the string, S is equal to 0 or not. If found to be true, print the value of N as the result.Otherwise, perform the following steps:Store the maximum number of consecutive 0s in the given string in a variable, say X.Initialize two variables, start as 0 and end as N-1.Increment the value of cnt and start by 1 while S[start] is not equal to '1'.Increment the value of cnt and decrement end by 1 while S[end] is not equal to '1'.Print the maximum of X and cnt as a result. Below is the implementation of the above approach: C++ // C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the maximum sum of // consecutive 0s present at the start // and end of any rotation of the string str void findMaximumZeros(string str, int n) { // Stores the count of 0s int c0 = 0; for (int i = 0; i < n; ++i) { if (str[i] == '0') c0++; } // If the frequency of '1' is 0 if (c0 == n) { // Print n as the result cout << n; return; } // Stores the required sum int mx = 0; // Find the maximum consecutive // length of 0s present in the string int cnt = 0; for (int i = 0; i < n; i++) { if (str[i] == '0') cnt++; else { mx = max(mx, cnt); cnt = 0; } } // Update the overall maximum sum mx = max(mx, cnt); // Find the number of 0s present at // the start and end of the string int start = 0, end = n - 1; cnt = 0; // Update the count of 0s at the start while (str[start] != '1' && start < n) { cnt++; start++; } // Update the count of 0s at the end while (str[end] != '1' && end >= 0) { cnt++; end--; } // Update the maximum sum mx = max(mx, cnt); // Print the result cout << mx; } // Driver Code int main() { // Given string string s = "1001"; // Store the size of the string int n = s.size(); findMaximumZeros(s, n); return 0; } Output: 2 Time Complexity: O(N)Auxiliary Space: O(1) Please refer complete article on Maximum number of 0s placed consecutively at the start and end in any rotation of a Binary String for more details! Comment More infoAdvertise with us Next Article C++ Program to Find Maximum number of 0s placed consecutively at the start and end in any rotation of a Binary String kartik Follow Improve Article Tags : Strings Greedy Mathematical Technical Scripter C++ Programs C++ DSA Technical Scripter 2020 binary-string rotation +6 More Practice Tags : CPPGreedyMathematicalStrings Similar Reads C++ Program for Minimum rotations required to get the same string Given a string, we need to find the minimum number of rotations required to get the same string. Examples: Input : s = "geeks" Output : 5 Input : s = "aaaa" Output : 1 The idea is based on below post.A Program to check if strings are rotations of each other or not Step 1: Initialize result = 0 (Here 2 min read C++ Program for Longest subsequence of a number having same left and right rotation Given a numeric string S, the task is to find the maximum length of a subsequence having its left rotation equal to its right rotation. Examples: Input: S = "100210601" Output: 4 Explanation: The subsequence "0000" satisfies the necessary condition. The subsequence "1010" generates the string "0101" 4 min read Maximize the decimal equivalent by flipping only a contiguous set of 0s Given a binary number in the form of a string, the task is to print a binary equivalent obtained by flipping only one contiguous set of 0s such that the decimal equivalent of this binary number is maximum.Note: Do not assume any trailing zeroes in the start of the binary number i.e. "0101" is given 4 min read Lexicographically largest string possible by reversing substrings having even number of 1s Given a binary string S, the task is to convert the given string S to its lexicographic maximum form by reversing substrings having an even number of 1s. Examples: Input: S = "10101"Output: 11010Explanation:Reversing the substring {S[0], ..., S[2]} modifies S to "10101". Reversing the substring {S[1 6 min read Longest subsequence possible that starts and ends with 1 and filled with 0 in the middle Given a binary string s, the task is to find the length of the longest subsequence that can be divided into three substrings such that the first and third substrings are either empty or filled with 1 and the substring at the middle is either empty or filled with 0. Examples: Input: s = "1001" Output 7 min read Minimize count of flips required such that no substring of 0s have length exceeding K Given a binary string str of length N and an integer K where K is in the range (1 ? K ? N), the task is to find the minimum number of flips( conversion of 0s to 1 or vice versa) required to be performed on the given string such that the resulting string does not contain K or more zeros together. Exa 6 min read Java Program to Find Maximum number of 0s placed consecutively at the start and end in any rotation of a Binary String Given a binary string S of size N, the task is to maximize the sum of the count of consecutive 0s present at the start and end of any of the rotations of the given string S. Examples: Input: S = "1001"Output: 2Explanation:All possible rotations of the string are:"1001": Count of 0s at the start = 0; 6 min read Python3 Program to Find Maximum number of 0s placed consecutively at the start and end in any rotation of a Binary String Given a binary string S of size N, the task is to maximize the sum of the count of consecutive 0s present at the start and end of any of the rotations of the given string S.Examples:Input: S = "1001"Output: 2Explanation:All possible rotations of the string are:"1001": Count of 0s at the start = 0; a 5 min read Javascript Program to Find Maximum number of 0s placed consecutively at the start and end in any rotation of a Binary String Given a binary string S of size N, the task is to maximize the sum of the count of consecutive 0s present at the start and end of any of the rotations of the given string S. Examples: Input: S = "1001"Output: 2Explanation:All possible rotations of the string are:"1001": Count of 0s at the start = 0; 5 min read Maximum number of 0s placed consecutively at the start and end in any rotation of a Binary String Given a binary string S of size N, the task is to maximize the sum of the count of consecutive 0s present at the start and end of any of the rotations of the given string S. Examples: Input: S = "1001"Output: 2Explanation:All possible rotations of the string are:"1001": Count of 0s at the start = 0; 15+ min read Like