text: Minimum flips to remove any consecutive 3 0 s or 1 s in given Binary string | C ++ program for the above approach ; Function to find the minimum number of flips to make all three pairs of consecutive characters different ; Stores resultant count of pairs ; Base Case ; Iterate over the range [ 0 , N - 2 ] ; If the consecutive 3 numbers are the same then increment the count and the counter ; Return the answer ; Driver Code
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; int minFlips ( string str ) { int count = 0 ; if ( str . size ( ) <= 2 ) { return 0 ; } for ( int i = 0 ; i < str . size ( ) - 2 ; ) { if ( str [ i ] == str [ i + 1 ] && str [ i + 2 ] == str [ i + 1 ] ) { i = i + 3 ; count ++ ; } else { i ++ ; } } return count ; } int main ( ) { string S = "0011101" ; cout << minFlips ( S ) ; return 0 ; }