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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMinJumps ( string s ) { vector < int > ones ; int jumps = 0 , median = 0 , ind = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s [ i ] == '1' ) ones . push_back ( i ) ; } if ( ones . size ( ) == 0 ) return jumps ; median = ones [ ones . size ( ) / 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 ; } int main ( ) { string S = "00100000010011" ; cout << getMinJumps ( S ) << ' ' ; return 0 ; }