Check If It Is a Straight Line

You are given an array coordinates where coordinates[i] = [x, y] is a point in the plane. Return true if all of the points lie on one straight line, and false otherwise.

30:00

Check If It Is a Straight Line
easy
Free written solution
Topics
Companies

You are given an array coordinates where coordinates[i] = [x, y] is a point in the plane. Return true if all of the points lie on one straight line, and false otherwise.

Example 1:
Input: {"coordinates":[[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]}
Output: true
Constraints:
  • 2≤coordinates.length≤10002 \leq \text{coordinates.length} \leq 1000

  • coordinates[i].length==2\text{coordinates}[i].\text{length} == 2

  • −104≤coordinates[i][0],coordinates[i][1]≤104-10^4 \leq \text{coordinates}[i][0], \text{coordinates}[i][1] \leq 10^4

  • coordinates contains no duplicate point.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"coordinates":[[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]}

6 points. They lie on one line exactly when every point sits on the line through the first two.

012345
anchor (1, 2)
(dx, dy) = ?
on line: 0/6

cross-product test

(x, y) is on the line when dx·(y−y0) = dy·(x−x0)

Variables
VariableValue
n6
DepthFunction Call
Stack empty
0/11