Segregate 1 s and 0 s in separate halves of a Binary String | Java program for the above approach ; 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 ; Driver Code ; Given string ; Length of the string ; Prints the minimum count of operations required
import java . io . * ; import java . util . * ; class GFG { static void minOps ( String s , int N ) { int ans = 0 ; for ( int i = 1 ; i < N ; i ++ ) { if ( s . charAt ( i ) != s . charAt ( i - 1 ) ) { ans ++ ; } } if ( ans % 2 == 1 ) { System . out . print ( ( ans - 1 ) / 2 ) ; return ; } System . out . print ( ans / 2 ) ; } public static void main ( String [ ] args ) { String str = "01011100" ; int N = str . length ( ) ; minOps ( str , N ) ; } }