Maximum Consecutive Zeroes in Concatenated Binary String Last Updated : 07 Sep, 2022 Comments Improve Suggest changes Like Article Like Report You are given a binary string str of length n. Suppose you create another string of size n * k by concatenating k copies of str together. What is the maximum size of a substring of the concatenated string consisting only of 0's? Given that k > 1. Examples: Input : str = "110010", k = 2 Output : 2 String becomes 110010110010 after two concatenations. This string has two zeroes. Input : str = "00100110", k = 4 Output : 3 If the given string contains all zeroes then the answer is n * k. If S contains ones then the answer is either the maximum length of a substring of str containing only zeroes, or the sum between the length of the maximal prefix of S containing only zeroes and the length of the maximal suffix of str containing only zeroes. The last one must be computed only if k > 1. Implementation: C++ // C++ program to find maximum number // of consecutive zeroes after // concatenating a binary string #include<bits/stdc++.h> using namespace std; // returns the maximum size of a // substring consisting only of // zeroes after k concatenation int max_length_substring(string st, int n, int k) { // stores the maximum length // of the required substring int max_len = 0; int len = 0; for (int i = 0; i < n; ++i) { // if the current character is 0 if (st[i] == '0') len++; else len = 0; // stores maximum length of current // substrings with zeroes max_len = max(max_len, len); } // if the whole string is // filled with zero if (max_len == n) return n * k; int pref = 0, suff = 0; // computes the length of the maximal // prefix which contains only zeroes for (int i = 0; st[i] == '0'; ++i, ++pref); // computes the length of the maximal // suffix which contains only zeroes for (int i = n - 1; st[i] == '0'; --i, ++suff); // if more than 1 concatenations // are to be made if (k > 1) max_len = max(max_len, pref + suff); return max_len; } // Driver code int main() { int n = 6; int k = 3; string st = "110010"; int ans = max_length_substring(st, n, k); cout << ans; } // This code is contributed by ihritik Java // Java program to find maximum number of // consecutive zeroes after concatenating // a binary string class GFG { // returns the maximum size of a substring // consisting only of zeroes // after k concatenation static int max_length_substring(String st, int n, int k) { // stores the maximum length of the // required substring int max_len = 0; int len = 0; for (int i = 0; i < n; ++i) { // if the current character is 0 if (st.charAt(i) == '0') len++; else len = 0; // stores maximum length of current // substrings with zeroes max_len = Math.max(max_len, len); } // if the whole string is filled with zero if (max_len == n) return n * k; int pref = 0, suff = 0; // computes the length of the maximal // prefix which contains only zeroes for (int i = 0; st.charAt(i) == '0'; ++i, ++pref) ; // computes the length of the maximal // suffix which contains only zeroes for (int i = n - 1; st.charAt(i) == '0'; --i, ++suff) ; // if more than 1 concatenations are to be made if (k > 1) max_len = Math.max(max_len, pref + suff); return max_len; } // Driver code public static void main(String[] args) { int n = 6; int k = 3; String st = "110010"; int ans = max_length_substring(st, n, k); System.out.println(ans); } } Python3 # Python3 program to find maximum # number of consecutive zeroes # after concatenating a binary string # returns the maximum size of a # substring consisting only of # zeroes after k concatenation def max_length_substring(st, n, k): # stores the maximum length # of the required substring max_len = 0 len = 0 for i in range(0, n): # if the current character is 0 if (st[i] == '0'): len = len + 1; else: len = 0 # stores maximum length of # current substrings with zeroes max_len = max(max_len, len) # if the whole is filled # with zero if (max_len == n): return n * k pref = 0 suff = 0 # computes the length of the maximal # prefix which contains only zeroes i = 0 while(st[i] == '0'): i = i + 1 pref = pref + 1 # computes the length of the maximal # suffix which contains only zeroes i = n - 1 while(st[i] == '0'): i = i - 1 suff = suff + 1 # if more than 1 concatenations # are to be made if (k > 1): max_len = max(max_len, pref + suff) return max_len # Driver code n = 6 k = 3 st = "110010" ans = max_length_substring(st, n, k) print(ans) # This code is contributed by ihritik C# // C# program to find maximum number // of consecutive zeroes after // concatenating a binary string using System; class GFG { // returns the maximum size of // a substring consisting only // of zeroes after k concatenation static int max_length_substring(string st, int n, int k) { // stores the maximum length // of the required substring int max_len = 0; int len = 0; for (int i = 0; i < n; ++i) { // if the current character is 0 if (st[i] == '0') len++; else len = 0; // stores maximum length of current // substrings with zeroes max_len = Math.Max(max_len, len); } // if the whole string is // filled with zero if (max_len == n) return n * k; int pref = 0, suff = 0; // computes the length of the maximal // prefix which contains only zeroes for (int i = 0; st[i] == '0'; ++i, ++pref); // computes the length of the maximal // suffix which contains only zeroes for (int i = n - 1; st[i] == '0'; --i, ++suff); // if more than 1 concatenations // are to be made if (k > 1) max_len = Math.Max(max_len, pref + suff); return max_len; } // Driver code public static void Main(string[] args) { int n = 6; int k = 3; string st = "110010"; int ans = max_length_substring(st, n, k); Console.WriteLine(ans); } } // This code is contributed by ihritik PHP <?php // PHP program to find maximum number // of consecutive zeroes after // concatenating a binary string // returns the maximum size of a // substring consisting only of // zeroes after k concatenation function max_length_substring($st, $n, $k) { // stores the maximum length // of the required substring $max_len = 0; $len = 0; for ($i = 0; $i < $n; ++$i) { // if the current character is 0 if ($st[$i] == '0') $len++; else $len = 0; // stores maximum length of // current substrings with zeroes $max_len = max($max_len, $len); } // if the whole $is filled // with zero if ($max_len == $n) return $n * $k; $pref = 0; $suff = 0; // computes the length of the maximal // prefix which contains only zeroes for ($i = 0; $st[$i] == '0'; ++$i, ++$pref); // computes the length of the maximal // suffix which contains only zeroes for ($i = $n - 1; $st[$i] == '0'; --$i, ++$suff); // if more than 1 concatenations // are to be made if ($k > 1) $max_len = max($max_len, $pref + $suff); return $max_len; } // Driver code $n = 6; $k = 3; $st = "110010"; $ans = max_length_substring($st, $n, $k); echo $ans; // This code is contributed by ihritik ?> JavaScript //JavaScript program to find maximum // number of consecutive zeroes // after concatenating a binary string // returns the maximum size of a // substring consisting only of // zeroes after k concatenation function max_length_substring(st, n, k) { // stores the maximum length // of the required substring var max_len = 0; var len = 0; for (var i = 0; i < n; i++) { // if the current character is 0 if (st[i] == '0') len = len + 1; else len = 0; // stores maximum length of // current substrings with zeroes max_len = Math.max(max_len, len); } // if the whole is filled // with zero if (max_len == n) return n * k; var pref = 0; var suff = 0; // computes the length of the maximal // prefix which contains only zeroes var i = 0; while(st[i] == '0') { i = i + 1; pref = pref + 1; } // computes the length of the maximal // suffix which contains only zeroes i = n - 1; while(st[i] == '0') { i = i - 1; suff = suff + 1; } // if more than 1 concatenations // are to be made if (k > 1) max_len = Math.max(max_len, pref + suff); return max_len; } // Driver code var n = 6; var k = 3; var st = "110010"; //Function Call var ans = max_length_substring(st, n, k); console.log(ans); // This code is contributed by phasing17 Output2 Complexity Analysis: Time Complexity: O(N), where N represents the length of the given string.Auxiliary Space: O(1), no extra space is required, so it is a constant. Comment More infoAdvertise with us Next Article Maximum Consecutive Zeroes in Concatenated Binary String A AmanKumarSingh Follow Improve Article Tags : Strings DSA binary-string Practice Tags : Strings Similar Reads Count the maximum inversion count by concatenating the given Strings Given A, the number of "1" strings, B number of "10" strings, and C number of "0" strings. The task is to count the maximum inversion count by concatenating these strings Note: Inversion count is defined as the number of pairs (i, j) such that 0 ⤠i < j ⤠N-1 and S[i] = '1' and S[j] = '0'. Exampl 4 min read Maximum consecutive oneâs (or zeros) in a binary array Given an array arr[] consisting of only 0's and 1's, the task is to find the count of a maximum number of consecutive 1's or 0's present in the array.Examples : Input: arr[] = {1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1}Output: 4Explanation: The maximum number of consecutive 1's in the array is 4 from index 7 min read Maximum difference of zeros and ones in binary string Given a binary string of 0s and 1s. The task is to find the length of the substring which is having a maximum difference between the number of 0s and the number of 1s (number of 0s - number of 1s). In case of all 1s print -1. Examples: Input : S = "11000010001"Output : 6From index 2 to index 9, ther 15 min read Maximum Strings Concatenation Given an array of strings, where each string consists of lowercase characters from a to j. You need to find the maximum number of strings that can be concatenated together such that the resulting string can be made out of exactly k distinct characters. Example: Input: n = 4, str = [ "abc", "de", "fg 8 min read Concatenate strings in any order to get Maximum Number of "AB" Given an array of strings of length N, it is allowed to concatenate them in any order. Find the maximum possible number of occurrences of 'AB' in the resulting string. Examples: Input : N = 4, arr={ "BCA", "BGGGA", "JKA", "BALB" } Output : 3 Concatenate them in the order JKA + BGGA + BCA + BALB and 9 min read Construct a binary string following the given constraints Given three integers A, B and X. The task is to construct a binary string str which has exactly A number of 0's and B number of 1's provided there has to be at least X indices such that str[i] != str[i+1]. Inputs are such that there's always a valid solution. Examples: Input: A = 2, B = 2, X = 1 Out 7 min read Count of Binary strings having at most X consecutive 1s and Y consecutive 0s Given two integers N and M (1 ⤠N, M ⤠100) denoting the total number of 1s and 0s respectively. The task is to count the number of possible arrangements of these 0s and 1s such that any arrangement has at most X consecutive 1s and Y consecutive 0s (1 ⤠X, Y ⤠10). As the number of arrangements can 7 min read Minimum removal of consecutive similar characters required to empty a Binary String Given a binary string S of length N, the task is to find the minimum number of removal of adjacent similar characters required to empty the given binary string. Examples: Input: S = â1100011âOutput: 2Explanation:Operation 1: Removal of all 0s modifies S to â1111â.Operation 2: Removal of all remainin 8 min read Maximum difference of zeros and ones in binary string | Set 2 (O(n) time) Given a binary string of 0s and 1s. The task is to find the maximum difference between the number of 0s and number of 1s in any sub-string of the given binary string. That is maximize ( number of 0s - number of 1s ) for any sub-string in the given binary string. Examples: Input : S = "11000010001" O 7 min read Count number of binary strings without consecutive 1's Given a positive integer n, the task is to count all possible distinct binary strings of length n such that there are no consecutive 1's.Examples: Input: n = 3Output: 5Explanation: 5 strings are ("000", "001", "010", "100", "101").Input: n = 2Output: 3Explanation: 3 strings are ("00", "01", "10").Ta 15+ min read Like