🔥🔥 Easy to Understand Solution ❗❗ 2 Approaches ❗❗ TC: O(n), SC: O(1) ❗❗ Beats 100% 🔥🔥
Approach 1: Sort\n- Time complexity: O(n logn)\n- Space complexity: O(log n)\n\n# Code\njava []\nclass Solution {\n    public int minMovesToSeat(int[] seats, in
# Approach 1: Sort\n- Time complexity: O(n logn)\n- Space complexity: O(log n)\n\n# Code\n```java []\nclass Solution {\n    public int minMovesToSeat(int[] seats, int[] students) {\n        int minMoves = 0;\n \n        Arrays.sort(seats);\n        Arrays.sort(students);\n \n        for(int i = 0; i < seats.length; i++){\n            minMoves = minMoves + (Math.abs(seats[i] - students[i]));\n        }\n\n        return minMoves;\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        \n        min_moves = 0\n        for i in range(len(seats)):\n            min_moves += abs(seats[i] - students[i])\n        \n        return min_moves\n```\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        \n        int minMoves = 0;\n        \n        for (size_t i = 0; i < seats.size(); ++i) {\n            minMoves += abs(seats[i] - students[i]);\n        }\n        \n        return minMoves;\n    }\n};\n```\n\n\n\n# Approach 2: Counting sort and 2 pointer\n- Time complexity: O(n)\n- Space complexity: O(1)\n\n``` java []\nclass Solution {\n    public int minMovesToSeat(int[] seats, int[] students) {\n        int[] seatPosition = freqArray(seats, 101);\n        int[] studentPosition = freqArray(students, 101);\n        \n        int minMoves = getMinMoves(seatPosition, studentPosition);\n        \n        return minMoves;\n    }\n\n    public static int[] freqArray(int[] arr, int len){\n        int[] freq = new int[len];\n        \n        for (int i = 0; i < arr.length; i++){\n            freq[arr[i]]++;\n        }\n        \n        return freq;\n    }\n\n    public static int getMinMoves(int[] seat, int[] student){\n        int moves = 0;\n        int j = 1;\n\n        for (int i = 1; i < seat.length; ){\n            if(seat[i] == 0){\n                i++;\n            }\n            else if (student[j] == 0) {\n                j++;\n            }\n            else {\n                seat[i]--;\n                student[j]--;\n                moves = moves + Math.abs(i - j);\n            }\n        }\n        \n        return moves;\n    }\n}\n```\n\n``` Python []\nclass Solution:\n    def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:\n        seat_position = self.freq_array(seats, 101)\n        student_position = self.freq_array(students, 101)\n        \n        min_moves = self.get_min_moves(seat_position, student_position)\n        \n        return min_moves\n\n    def freq_array(self, arr, length):\n        freq = [0] * length\n        for val in arr:\n            freq[val] += 1\n        return freq\n\n    def get_min_moves(self, seat, student):\n        moves = 0\n        j = 1\n\n        for i in range(1, len(seat)):\n            while seat[i] > 0:\n                while student[j] == 0:\n                    j += 1\n                seat[i] -= 1\n                student[j] -= 1\n                moves += abs(i - j)\n        \n        return moves\n```\n\n``` C++ []\nclass Solution {\npublic:\n    int minMovesToSeat(vector<int>& seats, vector<int>& students) {\n        vector<int> seatPosition = freqArray(seats, 101);\n        vector<int> studentPosition = freqArray(students, 101);\n        \n        int minMoves = getMinMoves(seatPosition, studentPosition);\n        \n        return minMoves;\n    }\n\n    vector<int> freqArray(const vector<int>& arr, int len) {\n        vector<int> freq(len, 0);\n        \n        for (int val : arr) {\n            freq[val]++;\n        }\n        \n        return freq;\n    }\n\n    int getMinMoves(vector<int>& seat, vector<int>& student) {\n        int moves = 0;\n        int j = 1;\n\n        for (int i = 1; i < seat.size(); ) {\n            if (seat[i] == 0) {\n                i++;\n            } else if (student[j] == 0) {\n                j++;\n            } else {\n                moves += abs(i - j);\n                seat[i]--;\n                student[j]--;\n            }\n        }\n        \n        return moves;\n    }\n};\n```
['Array', 'Two Pointers', 'Greedy', 'Sorting', 'Counting Sort', 'Python', 'C++', 'Java', 'Python3']