Closest Pair


Collapse Content

Given a set of points on a 2D plane, can you find the closest two points and print out the distance between them?

For example, the two red dots are the closest points in the picture below.

Pair of points

Input Format

The first number will list the number of cases. Each case will consist of 2 lines:

  • N, the number of doubles (N/2 = the number of Points)
  • Followed by N doubles, where each 2 doubles form a Point.

The boilerplate code will provide all the numbers in a single array of doubles.

Output Format

Print out the distance between the 2 closest points, rounded to 5 decimal places.

Sample Input

20
0.8239934173914427 0.1319694539175018 0.8342200042902134 0.7131084191082707 0.09916950054781792 0.06385940977543703 0.9728160689903603 0.9866024736887545 0.9894053123073387 0.3231668870036569 0.6921178364907058 0.4819960661900089 0.08838176109695095 0.7032698234535882 0.22954065151820313 0.9502101028876844 0.5391591247928582 0.6384250501809671 0.9814608698617646 0.5369293900667655

Sample Output

0.21391

Explanation

(0.9814608698617646, 0.5369293900667655) and (0.9894053123073387, 0.3231668870036569) are the 2 closest points, with a distance of 0.21391 between them(rounded to 5 decimal places).

Challenge

Find the 2 closets points and print out the distance between them, rounded to 5 decimal places.

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Contact Us