Super Easy | Beats 98%
Intuition\n\nSorting both arrays allows for effiecient pairing of students to seats minimizing total movement required.\n\n# Approach: Greedy\n\n\n- Arrange bot
# Intuition\n\nSorting both arrays allows for effiecient pairing of students to seats minimizing total movement required.\n\n# Approach: Greedy\n\n\n- Arrange both arrays `seats` and `students` in ascending order.\n- Iterate through indices from `0` to the length of either array.\n- Calculate the absolute difference between `seats[i]` and `students[i]` at each index. This ensures the minimal number of moves needed to align all students with their corresponding seats.\n- Sorting both arrays simplifies the problem significantly.\n\n# Complexity\n- Time complexity: O(nlogn)\n\n- Space complexity: O(1)\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    int minMovesToSeat(vector<int>& seats, vector<int>& students) {\n        sort(begin(seats), end(seats));\n        sort(begin(students), end(students));\n        int moves = 0;\n\n        for(int i = 0; i < seats.size(); ++i) {\n            moves += abs(seats[i] - students[i]);\n        }\n        \n        return moves;\n    }\n};\n```\n```java []\nclass Solution {\n    public int minMovesToSeat(int[] seats, int[] students) {\n        Arrays.sort(seats);\n        Arrays.sort(students);\n        int moves = 0;\n\n        for(int i = 0; i < seats.length; ++i) {\n            moves += Math.abs(seats[i] - students[i]);\n        }\n\n        return moves;\n    }\n}\n```\n```python []\nclass Solution:\n    def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:\n        seats.sort()\n        students.sort()\n        moves = 0\n\n        for i in range(len(seats)) :\n            moves += abs(seats[i] - students[i])\n\n        return moves\n```\n```Go []\nfunc minMovesToSeat(seats []int, students []int) int {\n    sort.Ints(seats)\n    sort.Ints(students)\n    moves := 0\n\n    for i := 0; i < len(seats); i++ {\n        moves += abs(seats[i] - students[i])\n    }\n\n    return moves\n}\n\nfunc abs(x int) int {\n    if x < 0 {\n        return -x\n    }\n\n    return x\n}\n```\n\n---\n\n**A room full of seats and students with numbers that don\'t match? Sure, let\'s play musical chairs with math. Fun times! HEHEHEHE**\n\n![chopoer gif.gif](https://assets.leetcode.com/users/images/46898aef-0066-4ff3-84e4-3da1273fa5f2_1718251628.9610584.gif)\n
['Array', 'Greedy', 'Sorting', 'C++', 'Java', 'Go', 'Python3']