Check if Points Lie on a Straight Line

You are given an integer array coordinates, where each coordinates[i] = [x, y] represents a point in the 2D plane. Your task is to determine if all the given points lie on the same straight line in the XY-plane.

Return true if all points lie on the same straight line, otherwise return false.

Example 1:

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

Output: true

Example 2:

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

Output: false

Now Your Turn!

Pick the correct output for the given input

Input: coordinates = [[-1, 1], [0, 2], [1, 3], [2, 4]]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 2 <= coordinates.length <= 1000
  • coordinates[i].length == 2
  • -104 <= coordinates[i][0], coordinates[i][1] <= 104
  • coordinates contains no duplicate points.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
bool checkStraightLine(vector<vector<int>>& coordinates) {
// Your code goes here
}
};
Test Case

Input:

Coordinates