VOOZH about

URL: https://www.geeksforgeeks.org/dsa/closest-pair-of-points-using-divide-and-conquer-algorithm/

⇱ Minimum Distance between Two Points - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Minimum Distance between Two Points

Last Updated : 21 Apr, 2026

You are given an array of n distinct points in a 2D plane, where each point is represented by its coordinates (x , y). Now find the minimum Euclidean distance between any two distinct points.

Note: For two points A(px, qx) and B(py, qy) the distance Euclidean between them is:

Distance =

Examples:

Input: arr[] = [[ -1, -2 ], [ 0, 0 ], [ 1, 2 ], [ 2, 3 ]]
Output: 1.414214
Explanation: The smallest distance is between points (1, 2) and (2, 3), which is 1.414214.

Input: arr[] = [[ -2, -2 ], [ 1, 2 ], [ -1, 0 ], [ 3, 3 ]]
Output: 2.236068
Explanation: The smallest distance is between points (-2, -2) and (-1, 0), which is 2.236068.

[Naive Approach] Checking All Pairs - O(n^2) Time and O(1) Space

The idea is to check the distance between all pairs of points and store the minimum distance found.


Output
1.414214

[Expected Approach] Using Divide and Conquer – O(n log n) Time and O(n) Space

The idea is to use the divide and conquer technique, where the given points are recursively divided into two We compute the minimum distance in the left and right halves separately, and then check for closer pairs that lie across the dividing line.

Step By Step Implementation:

  • Sort the points by x-coordinate.
  • Recursively divide the points into left and right subarrays until each subarrays has one or two points:
    • If one point, return infinity.
    • If two points, return their distance.
  • Find the minimum distances dl and dr in the left and right subarrays, and set d as the smaller value between dl and dr.
👁 Image
  • Build a strip: Include points with x-distance ≤ d from midline
👁 Image
  • Sort the strip by y-coordinate.
  • For each point in the strip, Compare each point with next ≤ 7 points.
  • Update minimum distance, if a smaller distance is found in the strip.
  • Return the minimum among the left half, right half, or across the strip.

Key Insights:

After dividing the points into two halves and computing the minimum distances dl​ and dr​, we take:

d = min⁡(dl , dr)

Now, instead of checking all cross-pairs, we only focus on points near the dividing line, as only these can form a closer pair. We stores all points whose x-distance from the dividing point isd, i.e., points between x - d and x + d. So, all these points lie inside a vertical strip of width 2d along the x-axis.

  • Why do we consider only points within distance d from the mid line?

To efficiently find the closest pair across the dividing line, we only consider points that lie within a distance d from the midline. Specifically, we collect all points whose x-coordinate lies between x − d and x + d , forming a vertical strip of width 2d. This is because if two points are farther than d apart in the x-direction, their overall Euclidean distance will already be greater than d, so they cannot form a closer pair and can be safely ignored.

  • Why do we sort by y?

Sorting the strip by y-coordinate allows us to compare only nearby points in the vertical direction. This helps reduce unnecessary comparisons and ensures efficiency in the merging step.

  • Why are at most 7 comparisons needed for each point?

The 2d × d rectangle can be divided into two d × d squares, and each of these can be further divided into four smaller squares of size d / 2 × d / 2​.

  • The diagonal of these smaller squares (d/2 x d/2) is less than d/√2 which is less than d, so no two points can occupy the same small square (otherwise, they’d be closer than d, violating the earlier recursive result).
  • So, each d × d square can have at most 4 points, totaling at most 8 points in the full 2d × d rectangle.

Since we’re only comparing the current point with the others, that means at most 7 valid comparisons.


Output
Minimum distance = 1.414214

Applications:

  • Used to detect planes that are too close to each other, helping prevent potential collisions.
  • Used in motion planning to avoid obstacles by identifying the closest points in a robot’s path.
  • Helps in data classification and pattern recognition by finding nearest data points.
  • Used in optimizing placement of devices by identifying nearest neighbors for better communication.

Time Complexity:

  • Let the time complexity of the algorithm be T(n).
  • The algorithm divides the set of points into two halves -> 2T(n/2)
  • Building the strip takes -> O(n)
  • Dividing the Py[] array takes -> O(n)
  • Processing the strip takes -> O(n)

So, the recurrence relation becomes:

T(n) = 2T ( n / 2)+ O(n)+ O(n)+ O(n)
T(n) = 2T ( n / 2) + O(n)
T(n)= O(n logn)

Auxiliary Space : O(n) (for storing Px[], Py[], and strip arrays)

Comment