Find the minimum cost matching between arrays of integers Consider two sorted arrays of integers \$X\$ and \$Y\$ of size \$m\$ and \$n\$ respectively with \$m &lt; n\$.  For example \$ X = (1,4)\$, \$Y = (2,10,11)\$.

We say that a matching is some way of pairing each element of \$X\$ with an element of \$Y\$ in such a way that no two elements of \$X\$ are paired with the same element of  \$Y\$.  The cost of a matching is just the sum of the absolute values of the differences in the pairs. 

For example, with \$X = (7,11)\$, \$Y = (2,10,11)\$ we can make the pairs \$(7,2), (11,10)\$ which then has cost \$5+1 = 6\$.  If we had made the pairs \$(7,10), (11,11)\$ the cost would have been \$3+0 = 3\$.  If we had made the pairs \$(7,11), (11,10)\$ the cost would have been \$4+1 = 5\$.

As another example take \$X = (7,11,14)\$, \$Y = (2,10,11,18)\$. We can make the pairs \$(7,2), (11,10), (14,11)\$ for a cost of \$9\$.  The pairs \$(7,10), (11,11), (14,18)\$ cost \$7\$.

The task is to write code that, given two sorted arrays of integers \$X\$ and \$Y\$, computes a minimum cost matching.

Test cases

[1, 4],      [2, 10, 11]     =&gt; [[1, 2], [4, 10]]
[7, 11],     [2, 10, 11]     =&gt; [[7, 10], [11, 11]]
[7, 11, 14], [2, 10, 11, 18] =&gt; [[7, 10], [11, 11], [14, 18]]
Jelly, 15 14 12 11 bytes

Œ!ż€IASƊÞḢṁ


Try it online!


-1 byte thanks to Jonathan Allan
-1 byte thanks to Mr. Xcoder
-2 bytes thanks to an anonymous editor


Brute force. Takes input as \$Y\$ then \$X\$.

Œ!ż€IASƊÞḢṁ
Œ!                 All permutations of Y.
  ż€               Zip each of the permutations with X.

       ƊÞ          Sort by:
    I              Difference of each pair.
     A             Absolute value.
      S            Sum.
         Ḣ         Take the first matching.
          ṁ        Mold the result like X. Keeps only values up to the length 
                   of X which removes unpaired values from Y.