Minimum jumps required to make a group of persons sit together | Java 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
import java . util . * ; class GFG { static int MOD = ( int ) 1e9 + 7 ; static int minJumps ( String seats ) { Vector < Integer > position = new Vector < > ( ) ; int count = 0 ; int len = seats . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( seats . charAt ( i ) == ' x ' ) { position . add ( i - count ) ; count ++ ; } } if ( count == len count == 0 ) return 0 ; int med_index = ( count - 1 ) / 2 ; int med_val = position . get ( med_index ) ; int ans = 0 ; for ( int i = 0 ; i < position . size ( ) ; i ++ ) { ans = ( ans % MOD + Math . abs ( position . get ( i ) - med_val ) % MOD ) % MOD ; } return ans % MOD ; } public static void main ( String [ ] args ) { String S = " . . . . x . . xx . . . x . . " ; System . out . print ( minJumps ( S ) ) ; } }