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 ; 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 ; Given binary string ; Function call
function maxSum ( str ) { var maximumSum = 0 ; var totalOnes = 0 ; str . split ( ' ' ) . forEach ( c => { if ( c == ' ' ) totalOnes ++ ; } ) ; var zero = 0 , ones = 0 ; for ( var i = 0 ; str [ i ] ; i ++ ) { if ( str [ i ] == ' ' ) { zero ++ ; } else { ones ++ ; } maximumSum = Math . max ( maximumSum , zero + ( totalOnes - ones ) ) ; } return maximumSum ; } var str = " " ; document . write ( maxSum ( str ) ) ;