Check If It Is a Straight Line
EasyPrompt
You are given an array coordinates, coordinates[i] = [x, y], where [x, y] represents the coordinate of a point. Check if these points make a straight line in the XY plane.
Example 1:

Input: coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
Output: trueExample 2:

Input: coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]
Output: false
Constraints:
2 <= coordinates.length <= 1000coordinates[i].length == 2-10^4 <= coordinates[i][0], coordinates[i][1] <= 10^4coordinatescontains no duplicate point.
Approaches
2 approaches with complexity analysis and trade-offs.
This approach works by calculating the slope of the line formed by the first two points. It then iterates through the rest of the points, checking if the slope formed by each point and the first point is identical. This method requires special handling for vertical lines and is susceptible to floating-point precision errors.
Algorithm
-
- If
coordinates.length <= 2, returntrue.
- If
-
- Get the first two points
p0(x0, y0)andp1(x1, y1).
- Get the first two points
-
- Check if the line is vertical (
x1 - x0 == 0).
- Check if the line is vertical (
-
- If vertical, iterate from the third point and check if all
xcoordinates are equal tox0. Returnfalseif any differs.
- If vertical, iterate from the third point and check if all
-
- If not vertical, calculate the slope
m = (y1 - y0) / (x1 - x0)using floating-point division.
- If not vertical, calculate the slope
-
- Iterate from the third point
pi(xi, yi).
- Iterate from the third point
-
- Calculate the current slope
current_m = (yi - y0) / (xi - x0).
- Calculate the current slope
-
- If
current_m != m, returnfalse.
- If
-
- If the loop completes, return
true.
- If the loop completes, return
Walkthrough
The fundamental idea is that for a set of points to be on a straight line, the slope between any two pairs of points must be constant. We can simplify this by picking a reference point (e.g., the first point) and checking if the slope between this reference point and all other points is the same.
The algorithm first handles the trivial case where there are 2 or fewer points. Then, it calculates the slope between the first two points. A special case is made for vertical lines where the change in x is zero. For all other points, it calculates the slope with the first point and compares it to the reference slope. Due to the nature of floating-point numbers, direct comparison can be unreliable.
public class Solution { public boolean checkStraightLine(int[][] coordinates) { if (coordinates.length <= 2) { return true; } int x0 = coordinates[0][0]; int y0 = coordinates[0][1]; int x1 = coordinates[1][0]; int y1 = coordinates[1][1]; // Handle vertical line case if (x1 - x0 == 0) { for (int i = 2; i < coordinates.length; i++) { if (coordinates[i][0] != x0) { return false; } } return true; } // Calculate the reference slope double slope = (double)(y1 - y0) / (x1 - x0); // Check the slope for all other points for (int i = 2; i < coordinates.length; i++) { int xi = coordinates[i][0]; int yi = coordinates[i][1]; // Handle the case where the current pair of points form a vertical line if (xi - x0 == 0) { return false; // Since the main line is not vertical } double currentSlope = (double)(yi - y0) / (xi - x0); if (currentSlope != slope) { return false; } } return true; }}Complexity
Time
O(N), where N is the number of points in the `coordinates` array. We iterate through the array once.
Space
O(1), as we only use a constant number of variables to store coordinates and the slope.
Trade-offs
Pros
Conceptually straightforward for those familiar with the slope formula.
Cons
Using floating-point numbers for comparison is unreliable due to precision issues.
Requires a separate check to handle vertical lines, making the code more complex.
A more robust and efficient approach is to use the property of collinear points that the area of the triangle formed by any three points is zero. This can be checked using a cross-product formula, which avoids floating-point division and its associated problems. The condition (y2 - y1) / (x2 - x1) == (y3 - y1) / (x3 - x1) is transformed into (y2 - y1) * (x3 - x1) == (y3 - y1) * (x2 - x1) to avoid division.
Algorithm
-
- If
coordinates.length <= 2, returntrue.
- If
-
- Select the first two points
p0(x0, y0)andp1(x1, y1)as reference points.
- Select the first two points
-
- Calculate the initial differences
dx = x1 - x0anddy = y1 - y0.
- Calculate the initial differences
-
- Iterate through the rest of the points
pi(xi, yi)starting from the third point (i = 2).
- Iterate through the rest of the points
-
- For each point
pi, check if it satisfies the cross-multiplication equation:dy * (xi - x0) != dx * (yi - y0).
- For each point
-
- If the equation is not satisfied for any point, it means the point is not on the line defined by
p0andp1. Returnfalse.
- If the equation is not satisfied for any point, it means the point is not on the line defined by
-
- If the loop completes, all points are collinear. Return
true.
- If the loop completes, all points are collinear. Return
Walkthrough
To avoid the pitfalls of floating-point arithmetic and division by zero, we can rephrase the slope equality condition. Three points P0(x0, y0), P1(x1, y1), and Pi(xi, yi) are collinear if the slope between (P0, P1) is equal to the slope between (P0, Pi).
slope(P0, P1) = (y1 - y0) / (x1 - x0)
slope(P0, Pi) = (yi - y0) / (xi - x0)
Setting them equal: (y1 - y0) / (x1 - x0) = (yi - y0) / (xi - x0)
By cross-multiplying to eliminate division, we get:
(y1 - y0) * (xi - x0) = (yi - y0) * (x1 - x0)
This equation holds true for vertical, horizontal, and sloped lines and only involves integer arithmetic, making it robust and efficient.
public class Solution { public boolean checkStraightLine(int[][] coordinates) { if (coordinates.length <= 2) { return true; } int x0 = coordinates[0][0]; int y0 = coordinates[0][1]; int x1 = coordinates[1][0]; int y1 = coordinates[1][1]; int dx = x1 - x0; int dy = y1 - y0; for (int i = 2; i < coordinates.length; i++) { int xi = coordinates[i][0]; int yi = coordinates[i][1]; // Check if (y_i - y_0) * (x_1 - x_0) == (y_1 - y_0) * (x_i - x_0) // which is (yi - y0) * dx == dy * (xi - x0) if (dy * (xi - x0) != dx * (yi - y0)) { return false; } } return true; }}Complexity
Time
O(N), where N is the number of points. The algorithm involves a single pass through the input array.
Space
O(1). We only use a few integer variables to store the differences and coordinates, regardless of the input size.
Trade-offs
Pros
Avoids floating-point arithmetic, eliminating precision errors.
Handles all cases (vertical, horizontal, sloped lines) with a single, unified formula.
Generally faster than floating-point calculations.
Cons
The mathematical reasoning (cross-product) might be slightly less intuitive than direct slope calculation for some.
Solutions
Solution
class Solution { public boolean checkStraightLine ( int [][] coordinates ) { int x1 = coordinates [ 0 ][ 0 ], y1 = coordinates [ 0 ][ 1 ]; int x2 = coordinates [ 1 ][ 0 ], y2 = coordinates [ 1 ][ 1 ]; for ( int i = 2 ; i < coordinates . length ; ++ i ) { int x = coordinates [ i ][ 0 ], y = coordinates [ i ][ 1 ]; if (( x - x1 ) * ( y2 - y1 ) != ( y - y1 ) * ( x2 - x1 )) { return false ; } } return true ; } }class Solution { public: bool checkStraightLine ( vector < vector < int >>& coordinates ) { int x1 = coordinates [ 0 ][ 0 ], y1 = coordinates [ 0 ][ 1 ]; int x2 = coordinates [ 1 ][ 0 ], y2 = coordinates [ 1 ][ 1 ]; for ( int i = 2 ; i < coordinates . size (); ++ i ) { int x = coordinates [ i ][ 0 ], y = coordinates [ i ][ 1 ]; if (( x - x1 ) * ( y2 - y1 ) != ( y - y1 ) * ( x2 - x1 )) { return false ; } } return true ; } };class Solution : def checkStraightLine ( self , coordinates : List [ List [ int ]]) -> bool : x1 , y1 = coordinates [ 0 ] x2 , y2 = coordinates [ 1 ] for x , y in coordinates [ 2 :]: if ( x - x1 ) * ( y2 - y1 ) != ( y - y1 ) * ( x2 - x1 ): return False return TrueVideo walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.