Minimum jumps required to make a group of persons sit together | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int MOD = 1e9 + 7 ; int minJumps ( string seats ) { vector < int > position ; int count = 0 ; int len = seats . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( seats [ i ] == ' x ' ) { position . push_back ( i - count ) ; count ++ ; } } if ( count == len count == 0 ) return 0 ; int med_index = ( count - 1 ) / 2 ; int med_val = position [ med_index ] ; int ans = 0 ; for ( int i = 0 ; i < position . size ( ) ; i ++ ) { ans = ( ans % MOD + abs ( position [ i ] - med_val ) % MOD ) % MOD ; } return ans % MOD ; } int main ( ) { string S = " . . . . x . . xx . . . x . . " ; cout << minJumps ( S ) ; return 0 ; }