Answer: CARS24 On-Campus OA (2024) | Minimum Number of Moves to Seat Everyone
Answer · Posted Jun 2026
Approach To minimize the total movement: Sort both arrays. Match the smallest seat with the smallest student, second smallest with second smallest, and so on. Sum the absolute differences of corresponding positions. This greedy strategy guarantees the minimum total moves. Strategy Sort seats. Sort students. Iterate through both arrays. Add abs(seats[i] - students[i]) to the answer. Java Solution: import java.util.Arrays; class Solution { public int minMovesToSeat(int[] seats, int[] students) { Arrays.sort(seats); Arrays.sort(students); int moves = 0; for (int i = ...
The full answer & interview discussion are available to premium members.
Log in Create a free account