Google Interview Question
Java DevelopersCountry: United States
public static Point[] func(Point[] points) {
if (points == null || points.length <= 1) return null;
Point[] answer = new Point[2];
int length = points.length;
Point firstPoint = points[0];
int j = 0;
for (int i = 1; i < length; i++) {
Point tempPoint = points[i];
if (tempPoint.x < firstPoint.x) {
firstPoint = tempPoint;
j = i;
}
}
answer[0] = firstPoint;
answer[1] = points[(j+1)%length];
for (int i = 0; i < length; i++) {
boolean flag = checkDirection(answer[0], points[i], answer[1]);
if (flag) {
answer[1] = points[i];
}
}
return answer;
}
private static boolean checkDirection(Point p1, Point p2, Point p3) {
int value = ((p3.y - p2.y)*(p2.x-p1.x)) - ((p2.y - p1.y)*(p3.x-p2.x));
if (value > 0) return true;
return false;
}
static class Point {
int x;
int y;
Point (int xx, int yy) {
x = xx;
y = yy;
}
}
fix any arbitary point, find another point that results in the minimum/maximum slope
- Mehdi January 16, 2018