#1232
Easy Algorithms Check if it is a straight line
Array Math Geometry
40.0% acceptance
Feb 25, 2026
2698
295
You are given an integer 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
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn check_straight_line(coordinates: Vec<Vec<i32>>) -> bool {
let (x0, y0) = (coordinates[0][0], coordinates[0][1]);
let (dx, dy) = (coordinates[1][0] - x0, coordinates[1][1] - y0);
for i in 2..coordinates.len() {
let (x, y) = (coordinates[i][0] - x0, coordinates[i][1] - y0);
// Cross product: dx * y - dy * x == 0 means collinear
if dx * y - dy * x != 0 {
return false;
}
}
true
}
}