![]() |
VOOZH | about |
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.
Table of Content
The idea is to check the distance between all pairs of points and store the minimum distance found.
1.414214
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:
dl and dr in the left and right subarrays, and set d as the smaller value between dl and dr.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 is ≤
d, i.e., points betweenx - dandx + d. So, all these points lie inside a vertical strip of width2dalong the x-axis.
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.
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.
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 thand, violating the earlier recursive result).- So, each
d × dsquare can have at most 4 points, totaling at most 8 points in the full2d × drectangle.Since we’re only comparing the current point with the others, that means at most 7 valid comparisons.
Minimum distance = 1.414214
Applications:
Time Complexity:
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)