2 Easy Interview Approaches✅ || Brute force🔥|| Count Sort✅ || Beat 💯 || All language 🔥
\n\n\n# Approach -1 \n Describe your approach to solving the problem. \nBrute force - By Sorting\n\n# Complexity\n- Time complexity: O(nlogn)\n Add your time co
![Runtime complexity.png](https://assets.leetcode.com/users/images/f6c7b3cb-3975-454e-a9a3-9a0b16c3e490_1718249779.7590492.png)\n\n\n# Approach -1 \n<!-- Describe your approach to solving the problem. -->\nBrute force - By Sorting\n\n# Complexity\n- Time complexity: O(nlogn)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    int minMovesToSeat(vector<int>& seats, vector<int>& students) {\n        int ans=0;\n\n        sort(seats.begin(),seats.end());\n        sort(students.begin(),students.end()); \n\n        for(int i=0; i<seats.size(); i++)\n        {\n            ans+=abs(seats[i]-students[i]);\n        }\n\n        return ans;\n    }\n};\n```\n```java []\nclass Solution {\n\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            // Add the absolute value of the difference\n            // between the position of the seat and the student\n            moves += Math.abs(seats[i] - students[i]);\n        }\n        return moves;\n    }\n}\n```\n```python []\nclass Solution:\n    def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:\n        students.sort()\n        seats.sort()\n        moves = 0\n        for i in range(0, len(seats)):\n            # Add the absolute value of the difference\n            # between the position of the seat and the student\n            moves += abs(seats[i] - students[i])\n        return moves\n```\n\n\n# Approach - 2\n<!-- Describe your approach to solving the problem. -->\nBetter Approach - By Count Sort\n\n# Complexity\n- Time complexity: O(n+m)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(m)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    int minMovesToSeat(vector<int>& seats, vector<int>& students) {\n        // Find the maximum position in the arrays\n        int maxPosition = max(findMax(seats), findMax(students));\n    // Stores difference between number of seats and students at each\n        // position\n        vector<int> differences(maxPosition, 0);\n        // Count the available seats at each position\n        for (int position : seats) {\n            differences[position - 1]++;\n        }\n        // Remove a seat for each student at that position\n        for (int position : students) {\n            differences[position - 1]--;\n        }\n        // Calculate the number of moves needed to seat the students\n        int moves = 0;\n        int unmatched = 0;\n        for (int difference : differences) {\n            moves += abs(unmatched);\n            unmatched += difference;\n        }\n        return moves;\n    }\nprivate:\n    int findMax(const vector<int>& array) {\n        int maximum = 0;\n        for (int num : array) {\n            if (num > maximum) {\n                maximum = num;\n            }\n        }\n        return maximum;\n    }\n};\n```\n```java []\nclass Solution {\n\n    public int minMovesToSeat(int[] seats, int[] students) {\n        // Find the maximum position in the arrays\n        int maxPosition = Math.max(findMax(seats), findMax(students));\n\n        // Stores difference between number of seats and students at each position\n        int[] differences = new int[maxPosition];\n\n        // Count the available seats at each position\n        for (int position : seats) {\n            differences[position - 1]++;\n        }\n\n        // Remove a seat for each student at that position\n        for (int position : students) {\n            differences[position - 1]--;\n        }\n\n        // Calculate the number of moves needed to seat the students\n        int moves = 0;\n        int unmatched = 0;\n        for (int difference : differences) {\n            moves += Math.abs(unmatched);\n            unmatched += difference;\n        }\n\n        return moves;\n    }\n\n    private int findMax(int[] array) {\n        int maximum = 0;\n        for (int num : array) {\n            if (num > maximum) {\n                maximum = num;\n            }\n        }\n        return maximum;\n    }\n}\n```\n```python []\nclass Solution:\n    def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:\n        # Find the maximum position in the arrays\n        max_position = max(max(seats), max(students))\n\n        # Stores difference between number of seats and students at each position\n        differences = [0] * (max_position)\n\n        # Count the available seats at each position\n        for position in seats:\n            differences[position - 1] += 1\n\n        # Remove a seat for each student at that position\n        for position in students:\n            differences[position - 1] -= 1\n\n        # Caculate the number of moves needed to seat the students\n        moves = 0\n        unmatched = 0\n        for difference in differences:\n            moves += abs(unmatched)\n            unmatched += difference\n\n        return moves\n```\n![upvote cat3.png](https://assets.leetcode.com/users/images/bc22442a-beba-4431-8a75-22caa6558422_1718249834.6741297.png)\n\n\n
['Array', 'Swift', 'Sorting', 'Counting Sort', 'C++', 'Java', 'Python3']