Easy Java Solution | Sorting | Constant Space | Beats 100 💯✅
Minimum Moves to Seat Everyone\n\n## Problem Statement\nGiven two integer arrays, seats and students, of the same length, where seats[i] is the position of the 
# Minimum Moves to Seat Everyone\n\n## Problem Statement\nGiven two integer arrays, `seats` and `students`, of the same length, where `seats[i]` is the position of the `i`-th seat and `students[i]` is the position of the `i`-th student, your task is to move each student to a seat such that the total number of moves is minimized. A move is defined as moving a student from position `i` to position `j` which takes `|i - j|` moves. Return the minimum number of moves needed to seat all students.\n\n## Solution Explanation\n\n### Intuition\nTo minimize the total number of moves needed to seat all students, we need to match students with seats in such a way that minimizes the distance between each student and their assigned seat. The most effective way to do this is by sorting both arrays and pairing students and seats at corresponding positions. This approach ensures that each student is paired with the closest available seat on average, leading to the minimal total distance.\n\n**Steps:**\n1. **Sort both arrays**: Sort the `seats` array and the `students` array in non-decreasing order.\n2. **Compute the moves**: For each student, compute the absolute difference between their position and the corresponding seat\'s position. Sum these differences to get the total number of moves required.\n\n### Why Sorting Works\nBy sorting both arrays, we ensure that the smallest available seat is paired with the smallest student position, the next smallest seat with the next smallest student position, and so on. This greedy strategy guarantees the minimal total distance because any deviation (like pairing the smallest seat with a larger student position and vice versa) would lead to an increase in the total distance.\n\n### Time Complexity\n- **Sorting the Arrays**: The dominant factor in the time complexity is sorting both the `seats` and `students` arrays. Sorting each array takes `O(n log n)`, where `n` is the number of elements.\n- **Computing the Moves**: Calculating the total number of moves involves iterating through the arrays, which takes `O(n)`.\n\nThus, the overall time complexity is `O(n log n)` due to the sorting step.\n\n### Space Complexity\n- **In-Place Sorting**: The sorting operation is typically done in-place for arrays, which means it requires `O(1)` additional space for storage if we ignore the recursive stack space of the sorting algorithm (for example, in quicksort).\n- **Space for Variables**: Apart from the input arrays and a few variables, no extra space is needed for computation.\n\nTherefore, the space complexity is `O(1)`, assuming in-place sorting.\n\n# Code\n```\nclass Solution {\n    public int minMovesToSeat(int[] seats, int[] students) {\n        Arrays.sort(seats);\n        Arrays.sort(students);\n        int ans=0;\n        for(int i=0;i<seats.length;i++)\n            ans+=Math.abs(seats[i]-students[i]);\n        return ans;\n    }\n}\n```
['Array', 'Math', 'Sorting', 'Java']