# Minimum Number of Moves to Seat Everyone in Java

There are n seats and n students in a room. Given an array seats of length n, where seats[i] is the position of the ith seat and students of length n, where students[j] is the position of the jth student.

Increase or decrease the position of the ith student by 1 (i.e., moving the ith student from position x to x + 1 or x - 1)

Return the minimum number of moves required to move each student to a seat such that no two students are in the same seat.

**Example 1**

**Input:** seats = [3,1,5], students = [2,7,4]

**Output**: 4

**Explanation:** The students are moved as follows:

The first student is moved from position 2 to position 1 using 1 move.

The second student is moved from from position 7 to position 5 using 2 move.

The third student is moved from position 4 to position 3 using 1 move.

In total, 1 + 2 + 1 = 4 moves were used.

**Example 2**

**Input:** seats = [2, 2, 6, 6], students = [1, 3, 2, 6]

**Output:** 4

**Explanation:** The students are moved as follows:

The first student is moved from position 1 to position 2 using 1 move.

The second student is moved from position 3 to position 6 using 3 moves.

The third student is not moved.

The fourth student is not moved.

In total, 1 + 3 + 1 = 4 moves were used.

## Approach 1: Simulation Approach

It calculates the minimum number of moves required to seat everyone in a classroom. It uses a simulation approach where it sorts the arrays of seats and students, then iterates over each seat and its corresponding student, calculating the absolute difference between their positions and summing up these differences to get the total number of moves.

### Algorithm

**Step 1:** Sort the arrays of seats and students.

**Step 2:** Initialize a variable moves to store the total number of moves required, starting with 0.

**Step 3:** Iterate over each seat and its corresponding student:

**Step 3.1:** Calculate the absolute difference between the position of the seat and the position of the student.

**Step 3.2:** Add this difference to the moves variable.

**Step 4:** Return the value of moves as the minimum number of moves required to seat everyone.

**Filename:** MinimumMovesToSeatEveryoneUsingSimple.java

import java.util.Arrays;

public class MinimumMovesToSeatEveryoneUsingSimple {

// Simulation Approach

public static int minMovesToSeat(int[] seats, int[] students) {

// Sort the arrays to ensure seats and students are in ascending order

Arrays.sort(seats);

Arrays.sort(students);

// Initialize moves counter

int moves = 0;

// Iterate over each seat and its corresponding student

for (int i = 0; i < seats.length; i++) {

// Calculate the absolute difference between seat and student positions and add to moves

moves += Math.abs(seats[i] - students[i]);

}

// Return total moves required

return moves;

}

public static void main(String[] args) {

// Example input

int[] seats = {3, 1, 5};

int[] students = {2, 7, 4};

// Calculate minimum moves

int minMoves = minMovesToSeat(seats, students);

System.out.println("Minimum number of moves: " + minMoves);

}

}

**Output**

Minimum number of moves: 4

**Time Complexity:** The time complexity of this approach is O(n log n), where n is the number of seats (or students). This complexity arises from sorting the arrays of seats and students using the built-in Arrays.sort() method. The subsequent iteration over the arrays has a linear time complexity of O(n).

**Space Complexity:** The space complexity of this approach is O(n), where n is the number of seats (or students). This is due to the space required to store the sorted arrays of seats and students. The additional space required for variables and intermediate calculations is constant.

## Approach 2: Using Recursion

It calculates the minimum number of moves required to seat everyone in a classroom using a recursion approach. The program recursively explores all possible seating arrangements to find the arrangement that minimizes the total moves.

### Algorithm

**Step 1:** Define a recursive function minMovesToSeatRecursion that takes three parameters: the arrays of seats and students, and the current index of the student being seated.

**Step 1.1:** If all students have been seated (index equals the length of the students array), return 0.

**Step 2:** For each seat, calculate the move required to seat the current student at that seat, add it to the moves required to seat the remaining students, and find the minimum of these values.

**Step 3:** Return the minimum moves required.

**Step 4:** In the main function, call minMovesToSeatRecursion with initial parameters and print the result.

**Filename:** MinimumMovesToSeatEveryoneUsingRecursion.java

import java.util.Arrays;

public class MinimumMovesToSeatEveryoneUsingRecursion {

// Recursion approach

public static int minMovesToSeatRecursion(int[] seats, int[] students, int index) {

// Base case: if we have processed all students, return 0

if (index == students.length) {

return 0;

}

// Find the minimum moves required if the current student sits in each seat

int minMoves = Integer.MAX_VALUE;

for (int i = 0; i < seats.length; i++) {

int move = Math.abs(seats[i] - students[index]) + minMovesToSeatRecursion(seats, students, index + 1);

minMoves = Math.min(minMoves, move);

}

return minMoves;

}

public static void main(String[] args) {

int[] seats = { 3, 1, 5};

int[] students = {2,7,4};

int minMoves = minMovesToSeatRecursion(seats, students, 0);

System.out.println("Minimum number of moves: " + minMoves);

}

}

**Output**

Minimum number of moves: 4

**Time Complexity:** The time complexity of the recursion approach is exponential, O(n^m), where n is the number of seats and m is the number of students. This is because for each student, we consider all possible seating arrangements (number of seats) recursively.

**Space Complexity:** The space complexity of the recursion approach is O(m), where m is the number of students. This is because of the recursive calls on the call stack, which can go up to the number of students in the worst case.