Skip to content

Commit e0ec6c4

Browse files
Create Day 8 Check If It Is a Straight Line.cpp
1 parent 48017eb commit e0ec6c4

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
PROBLEM:
2+
3+
4+
You are given an array coordinates, coordinates[i] = [x, y], where [x, y] represents the coordinate of a point.
5+
Check if these points make a straight line in the XY plane.
6+
7+
Example 1:
8+
Input: coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
9+
Output: true
10+
11+
Example 2:
12+
Input: coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]
13+
Output: false
14+
15+
16+
Constraints:
17+
18+
2 <= coordinates.length <= 1000
19+
coordinates[i].length == 2
20+
-10^4 <= coordinates[i][0], coordinates[i][1] <= 10^4
21+
coordinates contains no duplicate point.
22+
23+
24+
25+
SOLUTION:
26+
27+
28+
29+
class Solution {
30+
public:
31+
bool checkStraightLine(vector<vector<int>>& coordinates) {
32+
int n=coordinates.size();
33+
int m=2,i;
34+
35+
if(n<3)
36+
{
37+
return true;
38+
}
39+
40+
for(i=0;i+2<n;i++)
41+
{
42+
if( (coordinates[i+1][0]-coordinates[i][0])*(coordinates[i+2][1]-coordinates[i+1][1]) == (coordinates[i+2][0]-coordinates[i+1][0])*(coordinates[i+1][1]-coordinates[i][1]) )
43+
{
44+
continue;
45+
}
46+
else
47+
return false;
48+
}
49+
50+
return true;
51+
}
52+
};

0 commit comments

Comments
 (0)