Minimize count of flips required such that no substring of 0 s have length exceeding K | C ++ program to implement the above approach ; Function to return minimum number of flips required ; Base Case ; Stores the count of minimum number of flips ; Stores the count of zeros in current substring ; If current character is 0 ; Continue ongoing substring ; Start a new substring ; If k consecutive zeroes are obtained ; End segment ; Return the result ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int min_flips ( string & str , int k ) { if ( str . size ( ) == 0 ) return 0 ; int ans = 0 ; int cnt_zeros = 0 ; for ( char ch : str ) { if ( ch == '0' ) { ++ cnt_zeros ; } else { cnt_zeros = 0 ; } if ( cnt_zeros == k ) { ++ ans ; cnt_zeros = 0 ; } } return ans ; } int main ( ) { string str = "11100000011" ; int k = 3 ; cout << min_flips ( str , k ) ; return 0 ; }