Suppose you are given two arrays of the same length $n$, say $a$ and $b$ containing unique positive integers. The L1 distance between $a$ and $b$ is defined as: $$d_1(a, b) = \sum_{i = 1}^n \lvert a_i - b_i \rvert$$ (assuming 1-indexing for convenience)
You are allowed to permute $a$ and $b$ and the goal is to minimize $d_1(a, b)$, or equivalently find a permutation which minimizes the L1 distance. As a corollary, you also need to prove that there is a unique mapping of elements of $a$ and $b$ for which this minimum is achieved, or equivalently, for a given permutation of $a$, only a unique permutation of $b$ can minimize the L1 distance.
My approach
The first trivial observation I made is that one can keep one of the arrays fixed and permute the other. So without loss of generality, let us keep $a$ and use the permutation where $a$ is sorted in increasing order and permute $b$. Taking several examples leads to the conclusion that $d_1(a, b)$ is minimized when $b$ is also sorted in increasing order. Take a few examples to demonstrate this fact:
- $a = \{1, 5, 7\}\quad b = \{6, 3, 2\}$. For various permutations of $b$, the L1 distances are:
- $b = \{2, 3, 6\}$, $d_1(a, b) = 4$
- $b = \{2, 6, 3\}$, $d_1(a, b) = 6$
- $b = \{3, 2, 6\}$, $d_1(a, b) = 6$
- $b = \{3, 6, 2\}$, $d_1(a, b) = 8$
- $b = \{6, 2, 3\}$, $d_1(a, b) = 13$
- $b = \{6, 3, 2\}$, $d_1(a, b) = 12$
- $a = \{1, 4, 5, 9\}\quad b = \{1, 3, 2, 6\}$. By programmatically listing down and calculating the L1 distance of all the permutations of $b$, one can prove that the minimum is achieved for the permutation where $b$ is sorted.
I tried to prove it by contradiction by assuming that $b$ is a sorted permutation, i.e. $b_1 \leq b_2 \leq \ldots b_n$ and $c$ is any other permutation. I am trying to prove that: $$ \left(d_1(a, b) > d_1(a, c) \right) \Rightarrow \text{Contradiction}$$ However, I haven't been able to proceed. How do I prove the claim?