text: Maximize count of 0 s in left and 1 s in right substring by splitting given Binary string | C ++ program for the above approach ; Function to maximize the sum of the count of zeros and ones in the left and right substring ; To store the total ones ; Count the total numbers of ones in string str ; To store the count of zeros and ones while traversing string ; Iterate the given string and update the maximum sum ; Update the maximum Sum ; Driver Code ; Given binary string ; Function call
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSum ( string & str ) { int maximumSum = 0 ; int totalOnes ; totalOnes = count ( str . begin ( ) , str . end ( ) , '1' ) ; int zero = 0 , ones = 0 ; for ( int i = 0 ; str [ i ] ; i ++ ) { if ( str [ i ] == '0' ) { zero ++ ; } else { ones ++ ; } maximumSum = max ( maximumSum , zero + ( totalOnes - ones ) ) ; } return maximumSum ; } int main ( ) { string str = "011101" ; cout << maxSum ( str ) ; return 0 ; }