Minimum jumps required to make a group of persons sit together
Given a string S of length N consisting of ‘x’ and ‘.’. The given string represents a row of seats where ‘x’ and ‘.’ represent occupied and unoccupied seats respectively. The task is to minimize the total number of hops or jumps to make all the occupants sit together i.e., next to each other, without having any vacant seat between them.
Note: Since the count of jumps can be very large, print the answer modulo 109 + 7.
Examples:
Input: S = “. . . . x . . x x . . . x . .”
Output: 5
Explanation: Below are the required shuffling of occupants:
Step 1: Make the person at 5th seat jump 2 places to the 7th seat.
Step 2: Make the person at 13th seat jump 3 places to the 10th seat.
Therefore, total number of jumps required = 2 + 3 = 5.
Input: S = “x x x . . . . . . . . x x x x x x”
Output: 24
Explanation: Move the occupants from 1st, 2nd and 3rd position to the 9th, 10th, 11th positions respectively. Therefore, the total number of jumps required = (11 – 3) + (10 – 2) + (9 – 3) = 24.
Approach: The idea is to use a Greedy Approach to solve this problem. Observe that it is always optimal to shift the elements towards the median element among the persons or the center person among all the persons present. The number of jumps will always be minimum when we shift points to the median. Below are the steps:
- Initialize a vector position to store the indexes of the persons present.
- Find the median of the vector position[]. All the other persons will now be made to sit around this person as this will give the minimum number of jumps that are required to be made.
- Initialize a variable ans that stores the minimum jumps required.
- Now, traverse the vector position[] and for every index i find the median element and update ans as:
ans= ans+ abs(position[i] – medianElement)
- After the above steps, print the value of ans as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
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;
}
|
Java
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));
}
}
|
Python3
MOD = 10 * * 9 + 7
def minJumps(seats):
position = []
count = 0
lenn = len (seats)
for i in range (lenn):
if (seats[i] = = 'x' ):
position.append(i - count)
count + = 1
if (count = = lenn or count = = 0 ):
return 0
med_index = (count - 1 ) / / 2
med_val = position[med_index]
ans = 0
for i in range ( len (position)):
ans = (ans % MOD +
abs (position[i] - med_val)
% MOD) % MOD
return ans % MOD
if __name__ = = '__main__' :
S = "....x..xx...x.."
print (minJumps(S))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int MOD = ( int )1e9 + 7;
static int minJumps(String seats)
{
List< int > position =
new List< int >();
int count = 0;
int len = seats.Length;
for ( int i = 0; i < len; i++)
{
if (seats[i] == 'x' )
{
position.Add(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.Count; i++)
{
ans = (ans % MOD +
Math.Abs(position[i] -
med_val) % MOD) % MOD;
}
return ans % MOD;
}
public static void Main(String[] args)
{
String S = "....x..xx...x.." ;
Console.Write(minJumps(S));
}
}
|
Javascript
<script>
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] == 'x' )
{
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 = "....x..xx...x.." ;
document.write(minJumps(S));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
26 Mar, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...