text: Minimum flips required to convert given string into concatenation of equal substrings of length K | C ++ Program to implement the above approach ; Function that returns the minimum number of flips to convert the s into a concatenation of K - length sub - string ; Stores the result ; Iterate through string index ; Stores count of 0 s & 1 s ; Iterate making K jumps ; Count 0 's ; Count 1 's ; Add minimum flips for index i ; Return minimum number of flips ; Driver Code
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperations ( string S , int K ) { int ans = 0 ; for ( int i = 0 ; i < K ; i ++ ) { int zero = 0 , one = 0 ; for ( int j = i ; j < S . size ( ) ; j += K ) { if ( S [ j ] == '0' ) zero ++ ; else one ++ ; } ans += min ( zero , one ) ; } return ans ; } int main ( ) { string S = "110100101" ; int K = 3 ; cout << minOperations ( S , K ) ; return 0 ; }