Minimum jumps required to make a group of persons sit together | Python3 program for the above approach ; Function to find the minimum jumps required to make the whole group sit adjacently ; Store the indexes ; Stores the count of occupants ; Length of the string ; Traverse the seats ; If current place is occupied ; Push the current position in the vector ; Base Case : ; The index of the median element ; The value of the median element ; Traverse the position [ ] ; Update the ans ; Return the final count ; Driver Code ; Given arrange of seats ; Function Call
MOD = 10 ** 9 + 7 NEW_LINE def minJumps ( seats ) : NEW_LINE INDENT position = [ ] NEW_LINE count = 0 NEW_LINE lenn = len ( seats ) NEW_LINE for i in range ( lenn ) : NEW_LINE INDENT if ( seats [ i ] == ' x ' ) : NEW_LINE INDENT position . append ( i - count ) NEW_LINE count += 1 NEW_LINE DEDENT DEDENT if ( count == lenn or count == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT med_index = ( count - 1 ) // 2 NEW_LINE med_val = position [ med_index ] NEW_LINE ans = 0 NEW_LINE for i in range ( len ( position ) ) : NEW_LINE INDENT ans = ( ans % MOD + abs ( position [ i ] - med_val ) % MOD ) % MOD NEW_LINE DEDENT return ans % MOD NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " . . . . x . . xx . . . x . . " NEW_LINE print ( minJumps ( S ) ) NEW_LINE DEDENT