Subtract the count of 1 from required number of ones
cnt_1 = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if ( S [ i ] == '0' ) : NEW_LINE INDENT cnt_1 += 1 NEW_LINE DEDENT DEDENT b = b - cnt_1 NEW_LINE