Minimize the number of moves required to seat each passenger in a chair
Last Updated :
14 Aug, 2023
You are a bus conductor with n chairs and n passengers. Given the array chairs[] and passengers[] representing their initial positions, your goal is to minimize the number of moves required to seat each passenger in a chair, ensuring that no two passengers occupy the same chair. You can move a passenger by increasing or decreasing their position by 1. Find the minimum number of moves needed to achieve the desired seating arrangement, considering any initial overlapping positions.
Examples:
Input: chairs = [3, 1, 5], passengers = [2, 7, 4]
Output: 4
Explanation:
- The first passenger is moved from position 2 to position 1
- The second passenger is moved from position 7 to position 5
- The third passenger is moved from position 4 to position 3
- Total moves: 1+2+1 = 4
Input: chairs = [2, 2, 6, 6], passengers = [1, 3, 2, 6]
Output: 4
Explanation:
- The first passenger is moved from position 1 to position 2
- The second passenger is moved from position 3 to position 6
- The third passenger is not moved
- The fourth passenger is not moved
- Total moves: 1+3+0+0 = 4
Approach: To solve the problem follow the below idea:
- The given problem can be solved efficiently using sorting.
- The number of moves can be calculated by taking the absolute of chairs[i] – passengers[i], where 0 < i < length of the array(n).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findMoves(vector< int >& chairs, vector< int >& passengers,
int n)
{
sort(chairs.begin(), chairs.end());
sort(passengers.begin(), passengers.end());
int ans = 0;
for ( int i = 0; i < n; i++) {
ans += abs (chairs[i] - passengers[i]);
}
return ans;
}
int main()
{
vector< int > chairs = { 3, 1, 5 };
vector< int > passengers = { 2, 7, 4 };
int n = 3;
cout << findMoves(chairs, passengers, n);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int findMoves( int [] chairs, int [] passengers,
int n)
{
Arrays.sort(chairs);
Arrays.sort(passengers);
int ans = 0 ;
for ( int i = 0 ; i < n; i++) {
ans += Math.abs(chairs[i] - passengers[i]);
}
return ans;
}
public static void main(String[] args)
{
int [] chairs = { 3 , 1 , 5 };
int [] passengers = { 2 , 7 , 4 };
int n = 3 ;
System.out.print(findMoves(chairs, passengers, n));
}
}
|
Python3
def findMoves(chairs, passengers, n):
chairs.sort()
passengers.sort()
ans = 0
for i in range (n):
ans + = abs (chairs[i] - passengers[i])
return ans
chairs = [ 3 , 1 , 5 ]
passengers = [ 2 , 7 , 4 ]
n = 3
print (findMoves(chairs, passengers, n))
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static int FindMoves(List< int > chairs,
List< int > passengers, int n)
{
chairs.Sort();
passengers.Sort();
int ans = 0;
for ( int i = 0; i < n; i++) {
ans += Math.Abs(chairs[i] - passengers[i]);
}
return ans;
}
public static void Main()
{
List< int > chairs = new List< int >{ 3, 1, 5 };
List< int > passengers = new List< int >{ 2, 7, 4 };
int n = 3;
Console.WriteLine(FindMoves(chairs, passengers, n));
}
}
|
Javascript
function findMoves(chairs, passengers, n) {
chairs.sort((a, b) => a - b);
passengers.sort((a, b) => a - b);
let ans = 0;
for (let i = 0; i < n; i++) {
ans += Math.abs(chairs[i] - passengers[i]);
}
return ans;
}
let chairs = [3, 1, 5];
let passengers = [2, 7, 4];
let n = 3;
console.log(findMoves(chairs, passengers, n));
|
Time Complexity: O(n*log n), n is the length of the array
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...