-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStraightLine_08May.java
More file actions
33 lines (30 loc) · 923 Bytes
/
StraightLine_08May.java
File metadata and controls
33 lines (30 loc) · 923 Bytes
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
/*
Solution -
1) Compute the slope from first point,
2) If all the points is making a straight line then their slope from first point will be same,
3) To copute slope between 2 points, use below formula
slope=(y2-y1)/(x2-x1)
*/
class Solution {
public boolean checkStraightLine(int[][] coordinates) {
if(coordinates.length ==2) {
return true;
}
int[] x = coordinates[0];
int[] y = coordinates[1];
float gSlope = findSlope(x,y);
for(int i=2;i<coordinates.length;i++) {
float slope = findSlope(x, coordinates[i]);
if(gSlope!=slope) {
return false;
}
}
return true;
}
private float findSlope(int[] x, int[] y) {
if((y[0]-x[0]) ==0) {
return 0;
}
return (float)(y[1] - x[1])/(y[0]-x[0]);
}
}