Beats 100% of users 💯 || Runtime 1ms 🎯||Two Approaches✌🏻|| Very Easy Solution✅
Intuition\n Describe your first thoughts on how to solve this problem. \nThe intution is very clear to us, we have to get the minimum number of moves required t
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intution is very clear to us, we have to get the minimum number of moves required to seat everyone, and for that we have to make both seats[] and students[] arrays equal(two arrays are equal if their elements are equal), and we have to get a minimum result hence we will use Greedy Algorithm.\n# Approach 1: Using Count Sort and Greedy Algorithm\n<!-- Describe your approach to solving the problem. -->\n1. **Determine the Maximum Values:** Find the maximum values in the seats[] and students[] arrays to determine the size of the counting arrays i.e. \'count1[]\' and \'count2[]\'needed.\n2. **Initialize Counting Arrays:** Create two counting arrays count1[] and count2 to store the frequency of each seats and students position.\n3. **Fill Counting Arrays:** Iterate through the seats[] and students[] arrays and update the counting arrays with the frequency of each value.\n4. **Sort the Arrays Using Counting Arrays:** Use the counting arrays count1[] and count2[] to sort the original seats and students arrays.\n5. **Calculate the Total Moves:** With both seats[] and students[] arrays sorted, calculate the total number of moves required to seat all students optimally,\nUsing a while loop, iterate through the sorted arrays. For each pair of corresponding elements in the seats[] and students[] arrays, calculate the absolute difference between the seat and student positions. This difference represents the number of moves needed for a student to sit in a particular seat, or this difference will make both the elements equal if added to the smaller one.\n**\'Math.abs(seats[a] - students[a])\'** computes the absolute difference between the positions.\n**\'count +=\'** ... accumulates the total moves required.\n**\'a++\'** increments the index to process the next pair of seats[] and students[].\n# Complexity\n- **Time complexity:**\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nSince this approach use counting sort, hence its time complexiety will be:\n\n$$O(n+k)$$ i.e. $$O(n)$$ i.e. linear time complexiety, here k is the maximum value in seats[] and students[] array.\n\n- **Space complexity:**\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nWe have declare two arrays count1[] and count2[] of size max1 and max2, hence its space complexity will be:\n    $$O(k)$$, here k is the maximum value in seats[] and students[] array.\n \n\n# Code\n```\nclass Solution {\n    public int minMovesToSeat(int[] seats, int[] students) {\n        int n = seats.length;\n        int max1 = 0, max2 = 0;\n        for(int i=0; i<n; i++){\n            max1 = Math.max(max1, seats[i]);\n            max2 = Math.max(max2, students[i]);\n        }\n        int count1[] = new int[max1+1];\n        int count2[] = new int[max2+1];\n        for(int i=0; i<n; i++){\n            count1[seats[i]]++;\n            count2[students[i]]++;\n        }\n        int a = 0;\n        for(int i=0; i<count1.length; i++){\n            while(count1[i] > 0){\n                seats[a] = i;\n                a++;\n                count1[i]--;\n            }\n        }\n        a = 0;\n        for(int i=0; i<count2.length; i++){\n            while(count2[i] > 0){\n                students[a] = i;\n                a++;\n                count2[i]--;\n            }\n        }\n        a = 0;\n        int count = 0;\n        while(a < n){\n            count += Math.abs(seats[a] - students[a]);\n            a++;\n        }\n        return count;\n    }\n}\n```\n\n# Approach 2: Using Inbuilt Sort(Greedy Algorithm)\n<!-- Describe your approach to solving the problem. -->\n1. **Sort Both Arrays:** The first step is to sort both the seats[] and students[] arrays. Sorting is crucial because it allows us to pair each student with the nearest available seat, ensuring that the total number of moves (the sum of the absolute differences between each seat and student position) is minimum.\n2. **Initialize Variables:** The variable \'count\' is initialized to zero. This will keep track of the total number of moves required. The variable \'i\' is also initialized to zero and will be used as an index to iterate through the arrays.\n3. **Calculate Moves:** Using a while loop, iterate through the sorted arrays. For each pair of corresponding elements in the seats[] and students[] arrays, calculate the absolute difference between the seat and student positions. This difference represents the number of moves needed for a student to sit in a particular seat, or this difference will make both the elements equal if added to the smaller one.\n**\'Math.abs(seats[i] - students[i])\'** computes the absolute difference between the positions.\n**\'count +=\'** ... accumulates the total moves required.\n**\'i++\'** increments the index to process the next pair of seats[] and students[].\n# Complexity\n- **Time complexity:**\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nSince it use inbuilt sort from java collection framework, hence its time complexity will be:\n        $$O(nlogn)$$\n\n- **Space complexity:**\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nAs this solution did not used any extra space, so its space complexity will be constant i.e. \n    O(1)\n\n# Code\n```\nclass Solution {\n    public int minMovesToSeat(int[] seats, int[] students) {\n        int n = seats.length;\n        Arrays.sort(seats);\n        Arrays.sort(students);\n        int count = 0, i = 0;\n        while(i < n){\n            count += Math.abs(seats[i] - students[i]);\n            i++;\n        }\n        return count;\n    }\n}\n```\n
['Array', 'Greedy', 'Sorting', 'Counting', 'Counting Sort', 'Java']