## The challenge

Given a number of points on a plane, your task is to find two points with the smallest distance between them in linearithmic O(n log n) time.

### Example

 `````` 1 2 3 4 5 6 7 8 9 10 `````` `````` 1 2 3 4 5 6 7 8 9 1 2 . A 3 . D 4 . F 5 . C 6 7 . E 8 . B 9 . G ``````

For the plane above, the input will be:

 `````` 1 2 3 4 5 6 7 8 9 10 11 `````` ``````[ [2,2], // A [2,8], // B [5,5], // C [6,3], // D [6,7], // E [7,4], // F [7,9] // G ] => closest pair is: [[6,3],[7,4]] or [[7,4],[6,3]] (both answers are valid) ``````

The two points that are closest to each other are D and F.
Expected answer should be an array with both points in any order.

### Goal

The goal is to come up with a function that can find two closest points for any arbitrary array of points, in a linearithmic time.

`Point` class is preloaded for you as:

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 `````` ``````public class Point { public double x, y; public Point() { x = y = 0.0; } public Point(double x, double y) { this.x = x; this.y = y; } @Override public String toString() { return String.format("(%f, %f)", x, y); } @Override public int hashCode() { return Double.hashCode(x) ^ Double.hashCode(y); } @Override public boolean equals(Object obj) { if (obj instanceof Point) { Point other = (Point) obj; return x == other.x && y == other.y; } else { return false; } } } ``````

## The solution in Java code

Option 1:

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 `````` ``````import java.util.*; import java.lang.*; public class Solution { public static List closestPair(List points) { final List pair = new ArrayList(); final List arr = new ArrayList<>(points); arr.sort((a, b) -> Double.valueOf(a.x).compareTo(Double.valueOf(b.x))); final int n = points.size(); double l = Double.valueOf(Integer.MAX_VALUE); double tolerance = Math.sqrt(l); int a = 0, b = 0; for (int i = 0; i + 1 < n; i++) { for (int j = i + 1; j < n; j++) { if (arr.get(j).x >= arr.get(i).x + tolerance) break; final double ls = Math.pow(arr.get(i).x - arr.get(j).x, 2) + Math.pow(arr.get(i).y - arr.get(j).y, 2); if (ls < l) { l = ls; tolerance = Math.sqrt(l); a = i; b = j; } } } pair.add(arr.get(a)); pair.add(arr.get(b)); return pair; } } ``````

Option 2:

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 `````` ``````import java.util.Arrays; import java.util.*; import java.util.stream.Collectors; public class Solution { public static List closestPair(List points) { return divideAndConquer(points.stream().sorted((o1, o2) -> Double.compare(o1.x,o2.x)) .collect(Collectors.toList()).toArray(new Point[points.size()]),points.size()); } private static List divideAndConquer(Point[] pointSet, int size){ if(size<=3){ double minDist=Double.MAX_VALUE; Point aMin = null; Point bMin = null; for(int i=0; i lMinPoints = divideAndConquer(Arrays.copyOfRange(pointSet, 0, mid), mid); List rMinPoints = divideAndConquer(Arrays.copyOfRange(pointSet, mid, size), size-mid); double mDistL = calculateDist(lMinPoints.get(0),lMinPoints.get(1)); double mDistR = calculateDist(rMinPoints.get(0),rMinPoints.get(1)); Point aMin = null; Point bMin = null; double minDist; if(mDistL Math.abs(midPoint.x-point.x) Double.compare(o1.y,o2.y)).toArray(Point[]::new); if(midSetSorted.length==0){ return Arrays.asList(aMin,bMin); } for(int i=0; i

## Test cases to validate our solution

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 `````` ``````import java.util.Arrays; import java.util.Comparator; import java.util.List; import org.junit.Assert; import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runners.MethodSorters; @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class SolutionTest { @Test public void test01_Example() { List points = Arrays.asList( new Point(2, 2), //A new Point(2, 8), //B new Point(5, 5), //C new Point(6, 3), //D new Point(6, 7), //E new Point(7, 4), //F new Point(7, 9) //G ); List result = Solution.closestPair(points); List expected = Arrays.asList(new Point(6, 3), new Point(7, 4)); verify(expected, result); } @Test public void test02_TwoPoints() { List points = Arrays.asList( new Point(2, 2), new Point(6, 3) ); List result = Solution.closestPair(points); List expected = Arrays.asList(new Point(6, 3), new Point(2, 2)); verify(expected, result); } @Test public void test03_DuplicatedPoint() { List points = Arrays.asList( new Point(2, 2), //A new Point(2, 8), //B new Point(5, 5), //C new Point(5, 5), //C new Point(6, 3), //D new Point(6, 7), //E new Point(7, 4), //F new Point(7, 9) //G ); List result = Solution.closestPair(points); List expected = Arrays.asList(new Point(5, 5), new Point(5,5)); verify(expected, result); } private void verify(List expected, List actual) { Comparator comparer = Comparator.comparingDouble(p -> p.x); Assert.assertNotNull("Returned array cannot be null.", actual); Assert.assertEquals("Expected exactly two points.", 2, actual.size()); Assert.assertFalse("Returned points must not be null.", actual.get(0) == null || actual.get(1) == null); expected.sort(comparer); actual.sort(comparer); boolean eq = expected.get(0).x == actual.get(0).x && expected.get(0).y == actual.get(0).y && expected.get(1).x == actual.get(1).x && expected.get(1).y == actual.get(1).y; Assert.assertTrue(String.format("Expected: %s, Actual: %s", expected.toString(), actual.toString()), eq); } } ``````