a few solutions
TL;DR\n\nSort the input arrays A and B and return the accumulated absolute difference of each ith element.\n\nAC:\n\n3 minutes 12 seconds during bi-weekly conte
**TL;DR**\n\nSort the input arrays `A` and `B` and return the accumulated absolute difference of each i<sup>th</sup> element.\n\n**AC:**\n\n3 minutes 12 seconds during [bi-weekly contest 63](https://leetcode.com/contest/biweekly-contest-63/ranking/82/)\n\n**Details:**\n\nThis sum is the optimal solution since each student position `a` in `A` is mapped onto each seat position `b` in `B`.  An informal proof by contradiction would be this thought experiment: consider the values of `A` and `B` in a two-dimensional plane, ie. a graph.  When `A` and `B` are sorted in ascending order, then their graph would represent monotonically non-decreasing values, ie. the lines which connect each point `a` in `A` start at the bottom-left of the graph and end at the top-right of the graph.  The same monotonically non-decreasing values can be observed from each point `b` in `B`.  So the graph looks something like this:\n\n![image](https://assets.leetcode.com/users/images/8ab30b62-b393-47b0-a424-1b708c745ea1_1634406996.2212293.png)\n\n\nThen we can visiualize the optimal solution as the mimimized absolute difference in the y-axis.  So the question then is: how do we minimize the difference between each i<sup>th</sup> element, ie. every a<sub>i</sub> and b<sub>j</sub> value for some i, j in 0..N-1 inclusive?  And we can draw little dotted lines which represent their absolute difference as the cost we must pay for the student/seating mapping:\n\n![image](https://assets.leetcode.com/users/images/3a0fa3ae-29b3-4d36-879a-aa44e001d193_1634407160.8059773.png)\n\nWithout loss of generality, we can safely swap each a<sub>i</sub> and b<sub>i</sub> value to make this graph "pretty", ie. more easy to visualize with all `b` in `B` at or above all `a` in `A` since we accumulate their absolute difference (per the commutative property `|a - b| == |b - a|`).  The contradictory thought is then: how could this accumulated absolute difference be further minimized?  It cannot; thus we have "proved" this is the optimal solution, since we have "shown" the opposite is impossible.\n\n---\n\n*Kotlin*\n```\nclass Solution {\n    var minMovesToSeat = { A: IntArray, B: IntArray -> (A.sorted() zip B.sorted()).map{ (a, b) -> Math.abs(a - b) }.sum() }\n}\n```\n\n*Java*\n```\nclass Solution {\n    public int minMovesToSeat(int[] A, int[] B) {\n        Arrays.sort(A);\n        Arrays.sort(B);\n        return IntStream.range(0, A.length)\n            .mapToObj(i -> Math.abs(A[i] - B[i]))\n            .reduce(0, (a, b) -> a + b);\n    }\n}\n```\n\n*Javascript*\n```\nlet minMovesToSeat = (A, B) => {\n    A.sort((a, b) => a - b);\n    B.sort((a, b) => a - b);\n    return _.sum(_.zip(A, B).map(([a, b]) => Math.abs(a - b)));\n};\n```\n\n*Python3*\n```\nclass Solution:\n    minMovesToSeat = lambda self, A, B: sum(abs(a - b) for a, b in zip(sorted(A), sorted(B)))\n```\n\n*Rust*\n```\ntype VI = Vec<i32>;\nimpl Solution {\n    pub fn min_moves_to_seat(mut A: VI, mut B: VI) -> i32 {\n        A.sort();\n        B.sort();\n        A.into_iter().zip(B.into_iter()).map(|(a, b)| (a - b).abs()).sum::<i32>()\n    }\n}\n```\n\n*C++*\n```\nclass Solution {\npublic:\n    using VI = vector<int>;\n    int minMovesToSeat(VI& A, VI& B, int best = 0) {\n        sort(A.begin(), A.end());\n        sort(B.begin(), B.end());\n        for (auto i{ 0 }; i < A.size(); ++i)\n            best += abs(A[i] - B[i]);\n        return best;\n    }\n};\n```
[]