Segregate 1 s and 0 s in separate halves of a Binary String | Function to count the minimum number of operations required to segregate all 1 s and 0 s in a binary string ; Stores the count of unequal pair of adjacent characters ; If an unequal pair of adjacent characters occurs ; For odd count ; For even count ; Given string ; Length of the string ; Prints the minimum count of operations required
function minOps ( s , N ) { var ans = 0 ; for ( i = 1 ; i < N ; i ++ ) { if ( s . charAt ( i ) != s . charAt ( i - 1 ) ) { ans ++ ; } } if ( ans % 2 == 1 ) { document . write ( ( ans - 1 ) / 2 ) ; return ; } document . write ( ans / 2 ) ; } var str = " " ; var N = str . length ; minOps ( str , N ) ;