arrays
permutations
distance optimization
algorithm
closest distance

Given two arrays, find the permutations that give closest distance between two arrays

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

If the goal is to permute one array so that it is as close as possible to another array under element-wise distance, you usually should not brute-force all permutations. For common distance measures such as sum of absolute differences or sum of squared differences, sorting both arrays in the same order gives the optimal pairing.

Why Brute Force Is the Wrong First Idea

A naive solution tries every permutation of one array and computes the distance to the other. That works only for tiny inputs because the search space is n!.

For example, a 10-element array already has more than 3.6 million permutations. That makes brute force a poor default unless the input is very small and purely educational.

Sort Both Arrays for Common Distance Objectives

For objectives like:

  • 'sum(abs(a_i - b_i))'
  • 'sum((a_i - b_i)^2)'

matching sorted values to sorted values is optimal.

python
1def min_absolute_distance_pairing(a, b):
2    a_sorted = sorted(a)
3    b_sorted = sorted(b)
4    distance = sum(abs(x - y) for x, y in zip(a_sorted, b_sorted))
5    return a_sorted, b_sorted, distance
6
7print(min_absolute_distance_pairing([4, 1, 8], [2, 6, 3]))

The reason is intuitive: large values should be paired with large values and small values with small values, instead of crossing the matches unnecessarily.

The “Best Permutation” Is the Sorting-Induced One

If the question specifically asks for the permutation of the first array, the answer is the ordering that results from sorting it consistently with the second array’s sorted order.

python
1a = [4, 1, 8]
2b = [2, 6, 3]
3
4best_a = sorted(a)
5best_b = sorted(b)
6print(best_a, best_b)

If you need the permutation relative to original indices, track those indices during sorting rather than generating every possible order.

Use Assignment Algorithms Only for More General Cost Functions

If your distance function is not one of the standard monotone element-wise cases, the problem becomes a more general assignment problem. In that case, you may need algorithms such as the Hungarian method rather than a simple sort.

That is a very different problem from “closest under absolute or squared difference,” so be clear about the cost definition before choosing the algorithm.

Common Pitfalls

  • Brute-forcing all permutations when sorting would solve the intended optimization problem directly.
  • Failing to define the distance metric precisely before choosing an algorithm.
  • Assuming the same strategy works for every arbitrary cost function.
  • Forgetting to preserve original indices when the required output is a permutation mapping rather than only the minimized distance.
  • Solving a matching problem as though it were only about generating permutations.

Summary

  • For common element-wise distances, sort both arrays in the same order.
  • Brute-force permutation search is usually unnecessary and too expensive.
  • The optimal permutation often comes directly from the sorted pairing.
  • Use assignment algorithms only when the cost function is more general than standard absolute or squared differences.
  • Define the distance metric first; the right algorithm follows from that definition.

Course illustration
Course illustration

All Rights Reserved.