Easiest Java Solution for Beginners [ 97.35 ] [ 2ms ]
Approach\n1. Sorting the Arrays:\n   - First, the seats array is sorted. This arranges the seats in ascending order based on their positions.\n   - Then, the st
# Approach\n1. **Sorting the Arrays**:\n   - First, the `seats` array is sorted. This arranges the seats in ascending order based on their positions.\n   - Then, the `students` array is sorted. This arranges the students in ascending order based on their current positions.\n\n2. **Initialization**:\n   - A variable `moves` is initialized to zero. This variable will keep track of the total number of moves required to seat all students.\n\n3. **Calculate Moves**:\n   - A loop runs from 0 to the length of the `seats` array. For each iteration:\n     - The absolute difference between the seat position (`seats[i]`) and the student position (`students[i]`) is calculated. This gives the number of moves required to move a student from their current position to the target seat position.\n     - This difference is added to the `moves` variable.\n\n4. **Return the Result**:\n   - After the loop completes, the total number of moves stored in `moves` is returned as the result.\n\n# Complexity\n- Time complexity:\nO(n log n)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution {\n    public int minMovesToSeat(int[] seats, int[] students) {\n        Arrays.sort(seats);\n        Arrays.sort(students);\n        int moves=0;\n        for(int i=0; i<seats.length; i++){\n            moves+=Math.abs(seats[i] - students[i]);\n        }\n        return moves;\n    }\n}\n```\n\n![193730892e8675ebafc11519f6174bee5c4a5ab0.jpeg](https://assets.leetcode.com/users/images/e28066fd-9f2f-465e-a387-3421e4fc7be1_1718519391.0958273.jpeg)\n
['Java']