-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMinimum DIstance Pair.java
More file actions
147 lines (95 loc) · 2.89 KB
/
Minimum DIstance Pair.java
File metadata and controls
147 lines (95 loc) · 2.89 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
Created by : aky91
SpaceTime : 25th Jan 18, @MNNIT
reference : https://www.geeksforgeeks.org/closest-pair-of-points/
*/
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
public class MinDistancePair {
public static class Point implements Comparable<Point> {
int x;
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public void print() {
System.out.println("" + this.x + " , " + this.y);
}
public int compareTo(Point comparePoint) {
int compareX = comparePoint.x;
return this.x - compareX;
}
public static Comparator<Point> ComparatorX = new Comparator<Point>() {
public int compare(Point p1, Point p2) {
// ascending order
return p1.x - p2.x;
}
};
public static Comparator<Point> ComparatorY = new Comparator<Point>() {
public int compare(Point p1, Point p2) {
// ascending order
return p1.y - p2.y;
}
};
}
public static Scanner scn = new Scanner(System.in);
public static void main(String[] args) throws java.lang.Exception {
System.out.print("Enter no. of points : ");
int N = scn.nextInt();
Point arr[] = new Point[N];
for (int i = 0; i < N; i++) {
System.out.print("Enter space seperated x,y for point " + (i + 1) + " : ");
int X = scn.nextInt();
int Y = scn.nextInt();
arr[i] = new Point(X, Y);
}
for (Point data : arr)
data.print();
//sorting with respect to x
Arrays.sort(arr, Point.ComparatorX);
System.out.println("Minimum distance : " + solver(arr, 0, arr.length - 1));
}
public static double solver(Point[] arr, int l, int r) {
if (l - r <= 2)
return trivialSolver(arr, l, r);
int mid = l + (r - l) / 2;
Point midPoint = arr[mid];
double ansl = solver(arr, l, mid);
double ansr = solver(arr, mid + 1, r);
double d = Math.min(ansl, ansr);
ArrayList<Point> strip = new ArrayList<>();
for (int i = l; i <= r; i++) {
if (Math.abs(arr[i].x - midPoint.x) < d)
strip.add(arr[i]);
}
return (double) Math.min(d, minDistStrip(strip, d));
}
public static double minDistStrip(ArrayList<Point> strip2, double d) {
Point[] strip = strip2.toArray(new Point[0]);
//sorting with respect to y
Arrays.sort(strip, Point.ComparatorY);
double min = d;
for (int i = 0; i < strip.length; i++)
for (int j = i + 1; j < strip.length && strip[i].y - strip[j].y < min; j++)
if (distance(strip[i], strip[j]) < min)
min = distance(strip[i], strip[j]);
return min;
}
public static double trivialSolver(Point[] arr, int l, int r) {
double min = Double.MAX_VALUE;
for (int i = l; i <= r; ++i)
for (int j = i + 1; j <= r; ++j)
if (distance(arr[i], arr[j]) < min)
min = distance(arr[i], arr[j]);
return min;
}
public static double distance(Point p1, Point p2) {
double X = Math.pow((p1.x - p2.x), 2);
double Y = Math.pow((p1.y - p2.y), 2);
double ans = Math.sqrt(X + Y);
return ans;
}
}