Minimum jumps required to make a group of persons sit together | Javascript 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 ; Given arrange of seats ; Function Call
let MOD = 1e9 + 7 ; function minJumps ( seats ) { let position = [ ] ; let count = 0 ; let len = seats . length ; for ( let i = 0 ; i < len ; i ++ ) { if ( seats [ i ] == ' ' ) { position . push ( i - count ) ; count ++ ; } } if ( count == len count == 0 ) return 0 ; let med_index = parseInt ( ( count - 1 ) / 2 , 10 ) ; let med_val = position [ med_index ] ; let ans = 0 ; for ( let i = 0 ; i < position . length ; i ++ ) { ans = ( ans % MOD + Math . abs ( position [ i ] - med_val ) % MOD ) % MOD ; } return ans % MOD ; } let S = " " ; document . write ( minJumps ( S ) ) ;