Split a Binary String such that count of 0 s and 1 s in left and right substrings is maximum | Function to find the maximum sum of count of 0 s in the left substring and count of 1 s in the right substring by splitting the string ; Stores count of 1 s the in binary string ; Traverse the binary string ; If current character is '1' ; Update cntOne ; Stores count of 0 s ; Stores count of 1 s ; Stores maximum sum of count of 0 s and 1 s by splitting the string ; Traverse the binary string ; If current character is '0' ; Update zero ; If current character is '1' ; Update one ; Update res ; Driver Code
function maxSumbySplittingstring ( str , N ) { var cntOne = 0 ; for ( var i = 0 ; i < N ; i ++ ) { if ( str [ i ] == ' ' ) { cntOne ++ ; } } var zero = 0 ; var one = 0 ; var res = 0 ; for ( var i = 0 ; i < N - 1 ; i ++ ) { if ( str [ i ] == ' ' ) { zero ++ ; } else { one ++ ; } res = Math . max ( res , zero + cntOne - one ) ; } return res ; } var str = " " ; var N = str . length ; document . write ( maxSumbySplittingstring ( str , N ) ) ;