Minimum jumps required to group all 1 s together in a given 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
function getMinJumps ( s ) { let ones = [ ] ; let jumps = 0 , median = 0 , ind = 0 ; for ( let i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] == ' ' ) ones . push ( i ) ; } if ( ones . length == 0 ) return jumps ; median = ones [ parseInt ( ones . length / 2 , 10 ) ] ; ind = median ; for ( let i = ind ; i >= 0 ; i -- ) { if ( s [ i ] == ' ' ) { jumps += ind - i ; ind -- ; } } ind = median ; for ( let i = ind ; i < s . length ; i ++ ) { if ( s [ i ] == ' ' ) { jumps += i - ind ; ind ++ ; } } return jumps ; } let S = " " ; document . write ( getMinJumps ( S ) ) ;