Closest Pair of Points

You are given an array containing 'N' points in the plane. The task is to find out the distance of the closest points.

Note :
Where distance between two points (x1, y1) and (x2, y2) is calculated as [(x1 - x2) ^ 2] + [(y1 - y2) ^ 2].
Input Format :
The first line contains a single integer 'N' denoting the number of points.

The next 'N' lines contain two integers separated by a single space, where the first integer represents the x coordinate and the second integer represents the y coordinate.
Output Format :
The only line contains the minimum distance between the 'N' points.

Note:

You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints :
2 <= 'N' <= 10^5
-10^5 <= 'x' <= 10^5 
-10^5 <= 'y' <= 10^5

Time Limit: 1 sec
CodingNinjas
author
2y

Algorithm:
1) Sort all points according to x coordinates.
2) Divide all points in two halves.
3) Recursively find the smallest distances in both subarrays.
4) Take the minimum of two smallest distances. L...read more

CodingNinjas
author
2y
Brute Force Approach
  1. For every pair of points (x1, y1) and (x2, y2), calculate the distance by the following formula:
    distance = (x1-x2)^2 + (y1-y2)^2.
  2. And if the minimum distance till now is greater tha...read more
CodingNinjas
author
2y
Divide And Conquer

We will use the Divide and Conquer Algorithm 

  1. Sort the points with respect to x coordinate and store it in the Pair array.
  2. Find the middle point in the sorted array, we can take Pair[n/2] as the middle point.
  3. Divide the given array into two halves. The first subarray contains points from Pair[0] to Pair[n/2]. The second subarray contains points from Pair[n/2+1] to Pair[n-1].
  4. Recursively find the smallest distances in both subarrays. Let the distances be leftDistance and rightDistance. Find the minimum of both. Let the minimum be minDistance. Our answer is this minDistance if both points of closest distance lie in any one half.
  5. Now we need to consider the pairs such that one point in the pair is from the left half and the other is from the right half. We will only consider those pairs of points which have a distance less than minDistance as we have already our minimum as minDistance.
  6. Consider the vertical line passing through the middle point and find all points whose x coordinate is closer than minDistance to the middle vertical line. Build an array Arr of all such points.
  7. Sort the array Arr according to y coordinates then for each point pi ∈ Arr consider all points pj which have y coordinates less than y coordinates of pi + minDistance, and for each pair calculate the distance and compare with the current best distance.
  8. At first glance, this is still a non-optimal algorithm: it seems that the number of pj for each pi which have y coordinates less than y coordinates of pi + minDistance will be of order N, and the required asymptotics will not work. However, surprisingly, it can be proved that the number of pairs is a quantity O(1), i.e. it does not exceed some small constant regardless of the points themselves. Proof of this fact can be found here - https://cp-algorithms.com/geometry/nearest_points.html
  9. Then return the minimum distance.
Space Complexity: O(nlogn)Explanation:

O(N*log(N)), where N is the length of the Array ARR.

 

As in every recursion stack, it uses strip array/list to store points which lie nearer to the middle point.

Time Complexity: OtherExplanation:

O(n*(Logn)^2), where N is the length of the Array ARR.

 

Let the Time complexity of the above algorithm be T(n). Let us assume that we use a O(nLogn) sorting algorithm. The above algorithm divides all points in two sets and recursively calls for two sets. After dividing, it finds the array Arr in O(n) time, sorts the array Arr in O(nLogn) time and finally finds the closest points in the array Arr in O(n) time. 

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

T(n) = O(n*(Logn)^2)

Add answer anonymously...
Snapdeal Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter