Input: A[] = {1, 2, 3, 4, 5}, B[] = {1, 2, 3, 4, 5}
Output: 12
Explanation:
One of the possible rearrangements of A[] is {5, 4, 3, 2, 1}.
One of the possible rearrangements of B[] is {1, 2, 3, 4, 4}.
Therefore, the sum of all possible values of abs(A[i] – B[i]) = { abs(5 – 1) + abs(4 – 2) + abs(3 – 3) + abs(2 – 4) + abs(1 – 5) } = 12
Input: A[] = {1, 2, 2, 4, 5}, B[] = {5, 5, 5, 6, 6}
Output: 13
Explanation:
One of the possible rearrangements of A[] is {5, 4, 2, 2, 1}.
One of the possible rearrangements of B[] is {5, 5, 5, 6, 6}.
Therefore, the sum of all possible values of abs(A[i] – B[i]) = { abs(5 – 5) + abs(4 – 5) + abs(2 – 5) + abs(2 – 6) + abs(1 – 6) } = 13