Optimum location of point to minimize total distance
Pattern: Binary search on the answer
Walk along the line with one number t. The total distance to the given points is a convex function of t, with a single valley, so ternary search on t finds its lowest point.
The problem
You are given a straight line a·x + b·y + c = 0 (with a and b not both 0) and a set of points. Find the point on
the line where the sum of straight-line distances to all the given points is smallest, and return that smallest sum.
For example, the line x - y - 3 = 0 with points (-3, -2), (-1, 0), (-1, 2), (1, 2), (3, 4) gives about 20.7652.
The link above is a GeeksforGeeks article on this problem. This guide's function minTotalDistance(a, b, c, points)
and its output, the smallest sum printed with 4 decimals, are its own. The article may ask for the point or print a
different precision.
Think first
- How can one number describe every point on the line?
- How does the distance from one fixed point change as you walk along the line?
- What happens when you add several such shapes?
Hints
Brute force, and why it fails
Try many evenly spaced values of t and keep the best: slow and only as precise as the spacing. Ternary search reaches
full precision in about 200 rounds of n distance sums.
The key insight
Binary search needs a yes/no boundary. Ternary search needs a single valley instead. Writing the line with one parameter turns a 2D question into a 1D convex one.
Dry run
| lo | -10000000 |
|---|---|
| hi | 10000000 |
The line is x - y - 3 = 0, and the points are (-3, -2), (-1, 0), (-1, 2), (1, 2) and (3, 4). A point on the line is (0, -3), and one step along it is (-1, -1) divided by √2.
The plan
start = a point on the line; step = (b, -a) / length
total(t) = sum over points of distance(start + t * step, point)
lo = -10^7, hi = 10^7
repeat 200 times:
m1 = lo + (hi - lo) / 3, m2 = hi - (hi - lo) / 3
if total(m1) < total(m2): hi = m2
else: lo = m1
return total((lo + hi) / 2)
Complexity
- Time
O(n × 200)- Space
O(1)
Each of the 200 rounds computes two totals of n distances.
Edge cases
- A vertical line (
b = 0): start atx = -c / aand step along y. - One point: the answer is its distance to the line.
- Points on the line itself: the answer can be 0 when there is only one, or the distance between them for two.
I've built my logic, show the code
Java
class Solution {
public double minTotalDistance(int a, int b, int c, int[][] points) {
// walk along the line: the point at step t is (x0 + t * dx, y0 + t * dy)
double x0 = b != 0 ? 0 : -(double) c / a, y0 = b != 0 ? -(double) c / b : 0;
double len = Math.sqrt((double) a * a + (double) b * b);
double dx = b / len, dy = -a / len; // a unit step along the line
double lo = -1e7, hi = 1e7;
for (int iter = 0; iter < 200; iter++) { // the total distance is convex in t
double m1 = lo + (hi - lo) / 3, m2 = hi - (hi - lo) / 3;
if (total(points, x0 + m1 * dx, y0 + m1 * dy) < total(points, x0 + m2 * dx, y0 + m2 * dy)) hi = m2;
else lo = m1;
}
double t = (lo + hi) / 2;
return total(points, x0 + t * dx, y0 + t * dy);
}
private double total(int[][] points, double x, double y) {
double sum = 0;
for (int[] p : points) sum += Math.hypot(x - p[0], y - p[1]);
return sum;
}
}C++
class Solution {
public:
double minTotalDistance(int a, int b, int c, vector<vector<int>>& points) {
// walk along the line: the point at step t is (x0 + t * dx, y0 + t * dy)
double x0 = b != 0 ? 0 : -static_cast<double>(c) / a, y0 = b != 0 ? -static_cast<double>(c) / b : 0;
double len = sqrt(static_cast<double>(a) * a + static_cast<double>(b) * b);
double dx = b / len, dy = -a / len; // a unit step along the line
double lo = -1e7, hi = 1e7;
for (int iter = 0; iter < 200; iter++) { // the total distance is convex in t
double m1 = lo + (hi - lo) / 3, m2 = hi - (hi - lo) / 3;
if (total(points, x0 + m1 * dx, y0 + m1 * dy) < total(points, x0 + m2 * dx, y0 + m2 * dy)) hi = m2;
else lo = m1;
}
double t = (lo + hi) / 2;
return total(points, x0 + t * dx, y0 + t * dy);
}
double total(vector<vector<int>>& points, double x, double y) {
double sum = 0;
for (const vector<int>& p : points) sum += hypot(x - p[0], y - p[1]);
return sum;
}
};Python
import math
class Solution:
def minTotalDistance(self, a: int, b: int, c: int, points: list[list[int]]) -> float:
# walk along the line: the point at step t is (x0 + t * dx, y0 + t * dy)
x0, y0 = (0.0, -c / b) if b != 0 else (-c / a, 0.0)
length = math.hypot(a, b)
dx, dy = b / length, -a / length # a unit step along the line
lo, hi = -1e7, 1e7
for _ in range(200): # the total distance is convex in t
m1, m2 = lo + (hi - lo) / 3, hi - (hi - lo) / 3
if self.total(points, x0 + m1 * dx, y0 + m1 * dy) < self.total(points, x0 + m2 * dx, y0 + m2 * dy):
hi = m2
else:
lo = m1
t = (lo + hi) / 2
return self.total(points, x0 + t * dx, y0 + t * dy)
def total(self, points: list[list[int]], x: float, y: float) -> float:
return sum(math.hypot(x - px, y - py) for px, py in points)Common mistakes
- Moving along
xand computingyfrom it, which fails for a vertical line. - Using a search range too small to contain the answer.
- Stopping when
lo == hi, which real numbers may never reach exactly.