🎉 One-stop destination for all your technical interview Preparation 🎉
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.
Slope = (y2 - y1) / (x2 - x1) = dy / dx
, where dy = y2 - y1, dx = x2 - x1
=> (y3-y1)(x2-x1) = (y2-y1)(x3-x1)
=> dx(y3-y1) = dy(x3-x1)`dy*(x3-x1) = dx*(y3-y1)
for all points.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;
}
};