Maximize count of 0 s in left and 1 s in right substring by splitting given Binary string | Function to maximize the sum of the count of zeros and ones in the left and right substring ; Count the total numbers of ones in str ; To store the count of zeros and ones while traversing string ; Iterate the given and update the maximum sum ; Update the maximum Sum ; Driver Code ; Given binary string ; Function call
def maxSum ( str ) : NEW_LINE INDENT maximumSum = 0 NEW_LINE totalOnes = 0 NEW_LINE for i in str : NEW_LINE INDENT if i == '1' : NEW_LINE INDENT totalOnes += 1 NEW_LINE DEDENT DEDENT zero = 0 NEW_LINE ones = 0 NEW_LINE i = 0 NEW_LINE while i < len ( str ) : NEW_LINE INDENT if ( str [ i ] == '0' ) : NEW_LINE INDENT zero += 1 NEW_LINE DEDENT else : NEW_LINE INDENT ones += 1 NEW_LINE DEDENT maximumSum = max ( maximumSum , zero + ( totalOnes - ones ) ) NEW_LINE i += 1 NEW_LINE DEDENT return maximumSum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = "011101" NEW_LINE print ( maxSum ( str ) ) NEW_LINE DEDENT