2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								id: 5951a53863c8a34f02bf1bdc
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								title: Closest-pair problem
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								challengeType: 5
							 
						 
					
						
							
								
									
										
										
										
											2019-08-05 09:17:33 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								forumTopicId: 302232
							 
						 
					
						
							
								
									
										
										
										
											2021-01-13 03:31:00 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								dashedName: closest-pair-problem
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --description--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-06-14 08:31:07 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Provide a function to find the closest two points among a set of given points in two dimensions.
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-06-14 08:31:07 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The straightforward solution is a $O(n^2)$ algorithm (which we can call *brute-force algorithm* ); the pseudo-code (using indexes) could be simply:
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< pre > < strong > bruteForceClosestPair< / strong >  of P(1), P(2), ... P(N) 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< strong > if</ strong >  N & #x3C ;  2 < strong > then</ strong >  
						 
					
						
							
								
									
										
										
										
											2019-06-14 20:04:16 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  < strong > return< / strong >  ∞
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< strong > else< / strong >  
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								  minDistance ← |P(1) - P(2)|
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  minPoints ← { P(1), P(2) }
							 
						 
					
						
							
								
									
										
										
										
											2019-06-14 20:04:16 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  < strong > foreach< / strong >  i ∈ [1, N-1]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    < strong > foreach< / strong >  j ∈ [i+1, N]
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								      < strong > if</ strong >  |P(i) - P(j)| & #x3C ;  minDistance < strong > then</ strong > 
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								        minDistance ← |P(i) - P(j)|
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        minPoints ← { P(i), P(j) }
							 
						 
					
						
							
								
									
										
										
										
											2019-06-14 20:04:16 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								      < strong > endif< / strong > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    < strong > endfor< / strong > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  < strong > endfor< / strong > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  < strong > return< / strong >  minDistance, minPoints
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< strong > endif< / strong >  
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								< / pre >  
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-06-14 08:31:07 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								A better algorithm is based on the recursive divide and conquer approach, which is $O(n\log n)$ a pseudo-code could be:
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< pre > < strong > closestPair< / strong >  of (xP, yP) 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								  where xP is P(1) .. P(N) sorted by x coordinate, and
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  yP is P(1) .. P(N) sorted by y coordinate (ascending order)
							 
						 
					
						
							
								
									
										
										
										
											2019-06-14 20:04:16 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< strong > if< / strong >  N ≤ 3 < strong > then< / strong >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  < strong > return< / strong >  closest points of xP using brute-force algorithm
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< strong > else< / strong >  
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								  xL ← points of xP from 1 to ⌈N/2⌉
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  xR ← points of xP from ⌈N/2⌉+1 to N
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  xm ← xP(⌈N/2⌉)< sub > x< / sub > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  yL ← { p ∈ yP : p< sub > x< / sub >  ≤ xm }
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  yR ← { p ∈ yP : p< sub > x< / sub >  > xm }
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								  (dL, pairL) ← closestPair of (xL, yL)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  (dR, pairR) ← closestPair of (xR, yR)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  (dmin, pairMin) ← (dR, pairR)
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  < strong > if</ strong >  dL & #x3C ;  dR < strong > then</ strong > 
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    (dmin, pairMin) ← (dL, pairL)
							 
						 
					
						
							
								
									
										
										
										
											2019-06-14 20:04:16 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  < strong > endif< / strong > 
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  yS ← { p ∈ yP : |xm - p< sub > x</ sub > | & #x3C ;  dmin }
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								  nS ← number of points in yS
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  (closest, closestPair) ← (dmin, pairMin)
							 
						 
					
						
							
								
									
										
										
										
											2019-06-14 20:04:16 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  < strong > for< / strong >  i < strong > from< / strong >  1 < strong > to< / strong >  nS - 1
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    k ← i + 1
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    < strong > while</ strong >  k ≤ nS < strong > and</ strong >  yS(k)< sub > y</ sub >  - yS(i)< sub > y</ sub >  & #x3C ;  dmin
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      < strong > if</ strong >  |yS(k) - yS(i)| & #x3C ;  closest < strong > then</ strong > 
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								        (closest, closestPair) ← (|yS(k) - yS(i)|, {yS(k), yS(i)})
							 
						 
					
						
							
								
									
										
										
										
											2019-06-14 20:04:16 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								      < strong > endif< / strong > 
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								      k ← k + 1
							 
						 
					
						
							
								
									
										
										
										
											2019-06-14 20:04:16 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    < strong > endwhile< / strong > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  < strong > endfor< / strong > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  < strong > return< / strong >  closest, closestPair
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< strong > endif< / strong >  
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								< / pre >  
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-06-14 08:31:07 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								For the input, expect the argument to be an array of `Point`  objects with `x`  and `y`  members set to numbers. Return an object containing the key:value pairs for `distance`  and `pair`  (the pair of two closest points).
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-06-14 08:31:07 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								For example `getClosestPair`  with input array `points` :
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const points = [
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(1, 2),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(3, 3),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(2, 2)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Would return:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  distance: 1,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  pair: [
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      x: 1,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      y: 2
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    },
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      x: 2,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      y: 2
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  ]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Note:** Sort the `pair`  array by their `x`  values in incrementing order.
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --hints--
  
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`getClosestPair`  should be a function. 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert(typeof getClosestPair === 'function');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-06-14 08:31:07 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`getClosestPair(points1).distance`  should be `0.0894096443343775` . 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert.equal(getClosestPair(points1).distance, answer1.distance);
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-06-14 08:31:07 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`getClosestPair(points1).pair`  should be `[ { x: 7.46489, y: 4.6268 }, { x: 7.46911, y: 4.71611 } ]` . 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert.deepEqual(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  JSON.parse(JSON.stringify(getClosestPair(points1))).pair,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  answer1.pair
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-06-14 08:31:07 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`getClosestPair(points2).distance`  should be `65.06919393998976` . 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								assert.equal(getClosestPair(points2).distance, answer2.distance);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-06-14 08:31:07 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`getClosestPair(points2).pair`  should be `[ { x: 37134, y: 1963 }, { x: 37181, y: 2008 } ]` . 
						 
					
						
							
								
									
										
										
										
											2020-09-15 09:57:40 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert.deepEqual(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  JSON.parse(JSON.stringify(getClosestPair(points2))).pair,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  answer2.pair
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								);
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-06-14 08:31:07 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`getClosestPair(points3).distance`  should be `6754.625082119658` . 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert.equal(getClosestPair(points3).distance, answer3.distance);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`getClosestPair(points3).pair`  should be `[ { x: 46817, y: 64975 }, { x: 48953, y: 58567 } ]` . 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert.deepEqual(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  JSON.parse(JSON.stringify(getClosestPair(points3))).pair,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  answer3.pair
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --seed--
  
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## --after-user-code--
  
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const points1 = [
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    new Point(0.748501, 4.09624),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    new Point(3.00302, 5.26164),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    new Point(3.61878,  9.52232),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    new Point(7.46911,  4.71611),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    new Point(5.7819,   2.69367),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    new Point(2.34709,  8.74782),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    new Point(2.87169,  5.97774),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    new Point(6.33101,  0.463131),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    new Point(7.46489,  4.6268),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    new Point(1.45428,  0.087596)
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const answer1 = {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  distance: 0.0894096443343775,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  pair: [
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      x: 7.46489,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      y: 4.6268
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    },
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      x: 7.46911,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      y: 4.71611
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  ]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-06-14 08:31:07 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const points2 = [
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(37100, 13118),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(37134, 1963),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(37181, 2008),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(37276, 21611),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(37307, 9320)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const answer2 = {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  distance: 65.06919393998976,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  pair: [
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      x: 37134,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      y: 1963
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    },
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      x: 37181,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      y: 2008
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  ]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-06-14 08:31:07 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const points3 = [
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(16910, 54699),
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  new Point(14773, 61107),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(95547, 45344),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(95951, 17573),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(5824, 41072),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(8769, 52562),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(21182, 41881),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(53226, 45749),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(68180, 887),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(29322, 44017),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(46817, 64975),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(10501, 483),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(57094, 60703),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(23318, 35472),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(72452, 88070),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(67775, 28659),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(19450, 20518),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(17314, 26927),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(98088, 11164),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(25050, 56835),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(8364, 6892),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(37868, 18382),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(23723, 7701),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(55767, 11569),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(70721, 66707),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(31863, 9837),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(49358, 30795),
							 
						 
					
						
							
								
									
										
										
										
											2021-06-14 08:31:07 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  new Point(13041, 39744),
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  new Point(59635, 26523),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(25859, 1292),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(1551, 53890),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(70316, 94479),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(48549, 86338),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(46413, 92747),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(27186, 50426),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(27591, 22655),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(10905, 46153),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(40408, 84202),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(52821, 73520),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(84865, 77388),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(99819, 32527),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(34404, 75657),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(78457, 96615),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(42140, 5564),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(62175, 92342),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(54958, 67112),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(4092, 19709),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(99415, 60298),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(51090, 52158),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  new Point(48953, 58567)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								];
							 
						 
					
						
							
								
									
										
										
										
											2021-06-14 08:31:07 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const answer3 = {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  distance: 6754.625082119658,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  pair: [
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      x: 46817,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      y: 64975
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    },
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      x: 48953,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      y: 58567
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  ]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## --seed-contents--
  
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const Point = function(x, y) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  this.x = x;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  this.y = y;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Point.prototype.getX = function() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return this.x;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Point.prototype.getY = function() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return this.y;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								function getClosestPair(pointsArr) {
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  return true;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --solutions--
  
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2019-02-26 17:07:07 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const Point = function(x, y) {
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								  this.x = x;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  this.y = y;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
									
										
										
										
											2019-02-26 17:07:07 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Point.prototype.getX = function() {
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								  return this.x;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
									
										
										
										
											2019-02-26 17:07:07 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Point.prototype.getY = function() {
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								  return this.y;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const mergeSort = function mergeSort(points, comp) {
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    if(points.length <  2 )  return  points ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var n = points.length,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        i = 0,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        j = 0,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        leftN = Math.floor(n / 2),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        rightN = leftN;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var leftPart = mergeSort( points.slice(0, leftN), comp),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        rightPart = mergeSort( points.slice(rightN), comp );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var sortedPart = [];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    while((i <  leftPart.length )  & &  ( j  <  rightPart . length ) )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        if(comp(leftPart[i], rightPart[j]) <  0 )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            sortedPart.push(leftPart[i]);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            i += 1;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            sortedPart.push(rightPart[j]);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            j += 1;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    while(i <  leftPart.length )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        sortedPart.push(leftPart[i]);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        i += 1;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    while(j <  rightPart.length )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        sortedPart.push(rightPart[j]);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        j += 1;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return sortedPart;
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const closestPair = function _closestPair(Px, Py) {
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    if(Px.length <  2 )  return  {  distance:  Infinity ,  pair:  [  new  Point ( 0 ,  0 ) ,  new  Point ( 0 ,  0 )  ]  } ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if(Px.length <  3 )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        //find euclid distance
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        var d = Math.sqrt( Math.pow(Math.abs(Px[1].x - Px[0].x), 2) + Math.pow(Math.abs(Px[1].y - Px[0].y), 2) );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        return {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            distance: d,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            pair: [ Px[0], Px[1] ]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        };
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var n = Px.length,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        leftN = Math.floor(n / 2),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        rightN = leftN;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var Xl = Px.slice(0, leftN),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        Xr = Px.slice(rightN),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        Xm = Xl[leftN - 1],
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        Yl = [],
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        Yr = [];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    //separate Py
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    for(var i = 0; i <  Py.length ;  i  + =  1 )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        if(Py[i].x < = Xm.x)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            Yl.push(Py[i]);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        else
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            Yr.push(Py[i]);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var dLeft = _closestPair(Xl, Yl),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        dRight = _closestPair(Xr, Yr);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var minDelta = dLeft.distance,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        closestPair = dLeft.pair;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if(dLeft.distance > dRight.distance) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        minDelta = dRight.distance;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        closestPair = dRight.pair;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    //filter points around Xm within delta (minDelta)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var closeY = [];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    for(i = 0; i <  Py.length ;  i  + =  1 )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        if(Math.abs(Py[i].x - Xm.x) <  minDelta )  closeY . push ( Py [ i ] ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    //find min within delta. 8 steps max
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    for(i = 0; i <  closeY.length ;  i  + =  1 )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        for(var j = i + 1; j <  Math.min (  ( i  +  8 ) ,  closeY . length  ) ;  j  + =  1 )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            var d = Math.sqrt( Math.pow(Math.abs(closeY[j].x - closeY[i].x), 2) + Math.pow(Math.abs(closeY[j].y - closeY[i].y), 2) );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            if(d <  minDelta )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                minDelta = d;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                closestPair = [ closeY[i], closeY[j] ]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        distance: minDelta,
							 
						 
					
						
							
								
									
										
										
										
											2021-06-14 08:31:07 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        pair: closestPair.sort((pointA, pointB) => pointA.x - pointB.x)
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    };
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-02-26 17:07:07 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								function getClosestPair(points) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const sortX = function(a, b) { return (a.x <  b.x )  ?  -1  :  ( ( a . x  >  b.x) ? 1 : 0); }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const sortY = function(a, b) { return (a.y <  b.y )  ?  -1  :  ( ( a . y  >  b.y) ? 1 : 0); }
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const Px = mergeSort(points, sortX);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const Py = mergeSort(points, sortY);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return closestPair(Px, Py);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```