Minimum jumps required to group all 1 s together in a given Binary string | C # 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
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { public static int getMinJumps ( string s ) { ArrayList ones = new ArrayList ( ) ; int jumps = 0 , median = 0 , ind = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == '1' ) ones . Add ( i ) ; } if ( ones . Count == 0 ) return jumps ; median = ( int ) ones [ ones . Count / 2 ] ; ind = median ; for ( int i = ind ; i >= 0 ; i -- ) { if ( s [ i ] == '1' ) { jumps += ind - i ; ind -- ; } } ind = median ; for ( int i = ind ; i < s . Length ; i ++ ) { if ( s [ i ] == '1' ) { jumps += i - ind ; ind ++ ; } } return jumps ; } public static void Main ( string [ ] args ) { string S = "00100000010011" ; Console . Write ( getMinJumps ( S ) ) ; } }