Maximum Consecutive Zeroes in Concatenated Binary String | C ++ 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 ; stores the maximum length of the required substring ; if the current character is 0 ; stores maximum length of current substrings with zeroes ; if the whole string is filled with zero ; computes the length of the maximal prefix which contains only zeroes ; computes the length of the maximal suffix which contains only zeroes ; if more than 1 concatenations are to be made ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int max_length_substring ( string st , int n , int k ) { int max_len = 0 ; int len = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( st [ i ] == '0' ) len ++ ; else len = 0 ; max_len = max ( max_len , len ) ; } if ( max_len == n ) return n * k ; int pref = 0 , suff = 0 ; for ( int i = 0 ; st [ i ] == '0' ; ++ i , ++ pref ) ; for ( int i = n - 1 ; st [ i ] == '0' ; -- i , ++ suff ) ; if ( k > 1 ) max_len = max ( max_len , pref + suff ) ; return max_len ; } int main ( ) { int n = 6 ; int k = 3 ; string st = "110010" ; int ans = max_length_substring ( st , n , k ) ; cout << ans ; }