Minimize removal of substring of 0 s to remove all occurrences of 0 s from a circular Binary String | C ++ program for the above approach ; Function to count minimum number of removal of consecutive 0 s required to make binary string consists only of 1 s ; Stores the count of removals ; Traverse the string S ; If the current character is '0' ; Traverse until consecutive characters are only '0' s ; If the binary string only contains 1 s , then return 1 ; If the first and the last characters are 0 ; Return the resultant count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minRemovals ( string str , int N ) { int ans = 0 ; bool X = false ; for ( int i = 0 ; i < N ; i ++ ) { if ( str [ i ] == '0' ) { ans ++ ; while ( str [ i ] == '0' ) { i ++ ; } } else { X = true ; } } if ( ! X ) return 1 ; if ( str [ 0 ] == '0' and str [ N - 1 ] == '0' ) { ans -- ; } return ans ; } int main ( ) { string S = "11010001" ; int N = S . size ( ) ; cout << minRemovals ( S , N ) ; return 0 ; }