Optimization from partial solution minimize sum of distances between pairs
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
When a problem asks you to pair items so that the total distance is as small as possible, the key is to identify the exact structure. In one dimension, pairing adjacent sorted values is often optimal. In more general settings, the problem becomes a matching or assignment problem, and a partial solution is only useful if it respects that structure.
Start with the Simplest Pairing Model
Suppose you have an even number of points on a line and want to split them into pairs so the sum of absolute distances is minimal. If the points are sorted, the optimal strategy is to pair neighbors.
For example, given:
[1, 3, 4, 10, 11, 13]
the best pairing is:
- '
(1, 3)' - '
(4, 10)is not good' - '
(4, 10)would force larger gaps later'
The optimal pairing is:
- '
(1, 3)' - '
(4, 10)? still not ideal'
Better yet, just pair adjacent sorted elements:
- '
(1, 3)' - '
(4, 10)? this example shows why we should sort and inspect carefully'
To make the principle clearer, with sorted values [1, 3, 4, 10, 11, 13], adjacent pairing gives:
- '
(1, 3)distance2' - '
(4, 10)distance6' - '
(11, 13)distance2'
Total 10
If you cross pairs such as (1, 10) and (3, 4), you immediately create a larger total. On a line, crossing pairings are never better than uncrossed pairings.
Implement the One-Dimensional Solution
For sorted one-dimensional values, the direct implementation is simple:
This works because on a line, once values are sorted, any crossing match can be uncrossed without increasing the total distance.
Where Partial Solutions Help
If you are building the answer incrementally, a good partial solution is usually one that commits only to local decisions that are known to be safe. In the one-dimensional case, pairing adjacent sorted items is such a safe decision.
In more complex versions of the problem, partial solutions often become states in a dynamic program. For example, if some items are already forced into pairs and others remain free, you can define the best possible completion from each prefix of the sorted input.
That is the real idea behind "optimization from partial solution": the partial solution should summarize enough information to finish the problem optimally without redoing all prior work.
General Case Becomes Matching
Once the points are not on a line, or the distance function is more complicated, the problem is no longer a simple greedy pairing problem. It becomes a minimum-weight matching or assignment problem.
For a bipartite case, you can build a cost matrix and solve it with an assignment algorithm:
The important distinction is that not every pairing problem has a greedy local rule. The one-dimensional absolute-distance case does, but many geometric and graph-based variants do not.
Recognize the Structure Before Choosing the Algorithm
A good workflow is:
- Check whether the points live on a line and distance is absolute difference.
- If yes, sort and pair adjacent values.
- If not, ask whether the problem is really an assignment or minimum-weight matching problem.
- Use dynamic programming or a matching algorithm when local greedy choices are not provably safe.
That reasoning is much stronger than grabbing a generic optimizer or clustering algorithm, which solves a different problem.
Common Pitfalls
- Treating a pairing problem as if it were clustering.
- Using a greedy rule in two-dimensional or graph-based cases without proof.
- Ignoring the sorted structure in the one-dimensional absolute-distance case.
- Assuming a partial solution is useful even when it does not capture enough state to finish optimally.
- Confusing "minimize total distance" with "minimize maximum distance", which can lead to different pairings.
Summary
- Pairing items to minimize total distance is a matching problem, not a clustering problem.
- In one dimension with absolute distance, sorting and pairing adjacent values is optimal.
- Partial solutions are useful when they summarize enough state for an optimal completion.
- More general versions require dynamic programming or matching algorithms.
- The first step is always to identify the exact structure of the distance and pairing constraints.
Related reading
- Optimize Divide an array into continuous subsequences of length no greater than k such that sum of maximum value of each subsequence is minimum
- Optimize finding index of nearest point in 2d arrays
- Optimize Leaper Graph algorithm?
- Optimized algorithm to schedule tasks with dependency?
- Optimization Techniques for C
- Optimize deep Q network with long episode
- Optimized argmin an effective way to find an item minimizing a function
- Optimized low-accuracy approximation to rootnx, n

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.