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
def getMinJumps ( s ) : NEW_LINE INDENT ones = [ ] NEW_LINE jumps , median , ind = 0 , 0 , 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT ones . append ( i ) NEW_LINE DEDENT DEDENT if ( len ( ones ) == 0 ) : NEW_LINE INDENT return jumps NEW_LINE DEDENT median = ones [ len ( ones ) // 2 ] NEW_LINE ind = median NEW_LINE for i in range ( ind , - 1 , - 1 ) : NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT jumps += ind - i NEW_LINE ind -= 1 NEW_LINE DEDENT DEDENT ind = median NEW_LINE for i in range ( ind , len ( s ) ) : NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT jumps += i - ind NEW_LINE ind += 1 NEW_LINE DEDENT DEDENT return jumps NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = "00100000010011" NEW_LINE print ( getMinJumps ( s ) ) NEW_LINE DEDENT