✏️ Easy 💯 beats || Detailed Explanation 🫀 || Formatted ✍🏻 || JAVA🍁|| C++🎲 || JS ☃️|| PY🍰|| GIT
\n# \uD83C\uDF89 Screenshot \uD83D\uDCF8\n\n\n\n\n## Input \uD83D\uDCE5 \n\n    Two Number Array (seats) && (students)\n\n    Seats[i] & Students[i] represent t
\n# \uD83C\uDF89 Screenshot \uD83D\uDCF8\n\n![2037.png](https://assets.leetcode.com/users/images/4ce51ee6-50ae-4534-a795-f1cc74441eb5_1718257043.7289815.png)\n\n\n## Input \uD83D\uDCE5 \n\n    Two Number Array (seats) && (students)\n\n    Seats[i] & Students[i] represent the position of Seats & Students\n\n\n## Output \uD83D\uDCE4\n\n    We have to count number of moves to place Student On Seat\n\n# Intuition \uD83E\uDD14\uD83D\uDCAD\n\n    I did\'t understand the problem first so just understand this first\n\n    - We are giving TWO array And both are telling that \n\n    - Seats array tells us where the seat is place \n     \n           --> (Seats position) \n\n    - Student array tells us where the student is standing \n     \n           --> (Student position)\n\n\n---\n \n    Now we have to calculate that what minimum number of moves \n\n    A Student take to sit it\'s nearest possible seat \n\n    Because student can move one step forword or backward\n\n# Example \uD83D\uDCDC\n\n    `Ex.`\n\n        Seats:  [3,1,5]  so seats are at  1 _ 3 _ 5\n\n        Student: [2,7,4] and student  _ 2 _ 4 _ _ _ 7\n\n        So If we need to place stu on minimum possible seat it will be\n\n        Student at _ will take _ seat \n\n                   2   ->  1  moves will => 2 - 1 = 1  \n\n                   4   ->  3  moves will =>  4 - 3 = 1\n\n                   7   ->  5  moves will =>  7 - 5 = 2 \n\n            Total will be 4\n\n\n\n\n# Approach \u270D\uD83C\uDFFC\n\n        There will be 2 steps \n\n    Step 1 -> First we\'ll sort both array   \n\n            seats.sort() & students.sort() \n\n            To find minimum possible positiion for every sturdent   \n\n    Step 2 -> Make Apply for loop upto seats.length & make moves = 0 \n\n            we will count for every moves \n\n            one student took to sit on his seat\n\n            Take abs diff of every Ith value simple \uD83D\uDE09\n\n\n\n# Complexity \uD83C\uDFD7\uFE0F\n- \u231A Time complexity: $$O(nlogn)$$ `For sorting both array`\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- \uD83E\uDDFA Space complexity: $$O(1)$$ \n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code \uD83D\uDC68\uD83C\uDFFB\u200D\uD83D\uDCBB\n\n\n``` JAVA []\nclass Solution {\n    public int minMovesToSeat(int[] seats, int[] students) {\n        Arrays.sort(seats);\n        Arrays.sort(students);\n\n        int moves = 0;\n\n        for (int i = 0; i < students.length; i++) {\n            moves += Math.abs(students[i] - seats[i]);\n        }\n\n        return moves;\n    }\n}\n```\n```C++ []\nclass Solution {\n    public int minMovesToSeat(int[] seats, int[] students) {\n        Arrays.sort(seats);\n        Arrays.sort(students);\n\n        int moves = 0;\n\n        for (int i = 0; i < students.length; i++) {\n            moves += Math.abs(students[i] - seats[i]);\n        }\n\n        return moves;\n    }\n}\n```\n``` JAVASCRIPT []\nvar minMovesToSeat = function (seats, students) {\n    seats.sort((a, b) => a - b)\n    students.sort((a, b) => a - b)\n\n    let moves = 0\n\n    for (let i = 0; i < seats.length; i++) {\n        moves += Math.abs(students[i] - seats[i]);\n    }\n\n    return moves;\n};\n```\n```PYTHON []\nclass Solution:\n    def minMovesToSeat(self, seats: List[int], \n                             students: List[int]) -> int:\n        seats.sort()\n        students.sort()\n\n        moves = 0\n\n        for i in range(len(seats)):\n            moves += abs(seats[i] - students[i])\n\n        return moves\n\n```\n\n
['Array', 'Greedy', 'Sorting', 'C++', 'Java', 'Python3', 'JavaScript']