Maximum length of consecutive 1 s or 0 s after flipping at most K characters | Function to find the maximum length continuos segment of character c after flipping at most K characters ; Stores the maximum length ; Stores the count of char ' c ' ; Start of window ; Remove the extra ' c ' from left ; Increment the value of the left ; Update the resultant maximum length of character ch ; Function to find the maximum length of consecutive 0 s or 1 s by flipping at most K characters of the string ; Print the maximum of the maximum length of 0 s or 1 s ; Driver Code
const maxLength = ( str , n , c , k ) => { let ans = - 1 ; let cnt = 0 ; let left = 0 ; for ( let right = 0 ; right < n ; right ++ ) { if ( str [ right ] == c ) { cnt ++ ; } while ( cnt > k ) { if ( str [ left ] == c ) { cnt -- ; } left ++ ; } ans = Math . max ( ans , right - left + 1 ) ; } return ans ; } const maxConsecutiveSegment = ( S , K ) => { let N = S . length ; return Math . max ( maxLength ( S , N , ' ' , K ) , maxLength ( S , N , ' ' , K ) ) ; } let S = " " ; let K = 1 ; document . write ( maxConsecutiveSegment ( S , K ) ) ;