Minimum Number of Moves to Seat Everyone

There are n available seats and n students in a room. seats[i] is the position of the ith seat and students[j] is the position of the jth student. In one move you can shift one student one position left or right. Return the minimum number of moves needed so that every student sits in a seat and no two students share a seat. Several seats or students may start at the same position.

30:00

Minimum Number of Moves to Seat Everyone
easy
Free written solution
Topics
Companies

There are n available seats and n students in a room. seats[i] is the position of the ith seat and students[j] is the position of the jth student. In one move you can shift one student one position left or right. Return the minimum number of moves needed so that every student sits in a seat and no two students share a seat. Several seats or students may start at the same position.

Example 1:
Input: {"seats":[3,1,5],"students":[2,7,4]}
Output: 4
Constraints:
  • n=seats.length=students.lengthn = \text{seats.length} = \text{students.length}

  • 1≤n≤1001 \leq n \leq 100

  • 1≤seats[i],students[j]≤1001 \leq \text{seats}[i], \text{students}[j] \leq 100

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"seats":[3,1,5],"students":[2,7,4]}

3 seats (squares) and 3 students (dots) on one row. Each step moves a student one position.

the row

squares: seats / dots: students

1

3

4

6

7

pairs (student -> seat)

2->3

unsorted

7->1

unsorted

4->5

unsorted

moves so far

0

Variables
VariableValue
moves0
DepthFunction Call
Stack empty
0/7