Minimum jumps required to group all 1 s together in a given Binary string | Java Program to find the minimum number of jumps required to group all ones together in the binary string ; Function to get the minimum jump value ; Store all indices of ones ; Populating one 's indices ; Calculate median ; Jumps required for 1 's  to the left of median ; Jumps required for 1 's  to the right of median ; Return the final answer ; Driver code
import java . io . * ; import java . util . * ; class GFG { public static int getMinJumps ( String s ) { Vector < Integer > ones = new Vector < Integer > ( ) ; int jumps = 0 , median = 0 , ind = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == '1' ) ones . add ( i ) ; } if ( ones . size ( ) == 0 ) return jumps ; median = ( int ) ones . get ( ones . size ( ) / 2 ) ; ind = median ; for ( int i = ind ; i >= 0 ; i -- ) { if ( s . charAt ( i ) == '1' ) { jumps += ind - i ; ind -- ; } } ind = median ; for ( int i = ind ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == '1' ) { jumps += i - ind ; ind ++ ; } } return jumps ; } public static void main ( String [ ] args ) { String S = "00100000010011" ; System . out . println ( getMinJumps ( S ) ) ; } }