Complete-Preparation

🎉 One-stop destination for all your technical interview Preparation 🎉

View the Project on GitHub

Check If It Is a Straight Line 🌟

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.

Solution

Code

class Solution {
public:
    bool checkStraightLine(vector<vector<int>>& c)
    {
        int n = c.size();
        int x1 = c[0][0], x2 = c[1][0];
        int y1 = c[0][1], y2 = c[1][1];
        int dy = y2 - y1, dx = x2 - x1;

        for (int i = 2; i < n; i++) {
            int x3 = c[i][0], y3 = c[i][1];
            if (dy * (x3 - x1) != dx * (y3 - y1))
                return false;
        }
        return true;
    }
};