Skip to content

Commit 52dfc43

Browse files
authored
Merge pull request #2271 from Hitesh4278/check-if-it-is-a-straight-line
Added the Solution of Check If It Is a Straight Line - Issue 2258
2 parents e4d6d47 + 407d009 commit 52dfc43

File tree

2 files changed

+190
-1
lines changed

2 files changed

+190
-1
lines changed

dsa-problems/leetcode-problems/1200-1299.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ export const problems = [
205205
"problemName": "1232. Check If It Is a Straight Line",
206206
"difficulty": "Easy",
207207
"leetCodeLink": "https://leetcode.com/problems/check-if-it-is-a-straight-line",
208-
"solutionLink": "#"
208+
"solutionLink": "/dsa-solutions/lc-solutions/1200-1299/check-if-it-is-a-straight-line"
209209
},
210210
{
211211
"problemName": "1233. Remove Sub-Folders from the Filesystem",
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
---
2+
id: check-if-it-is-a-straight-line
3+
title: 1232. Check If It Is a Straight Line
4+
sidebar_label: 1232. Check If It Is a Straight Line
5+
tags:
6+
- Array
7+
- Maths
8+
- Geometry
9+
10+
description: "This is a solution to the Check If It Is a Straight Line problem on LeetCode."
11+
---
12+
13+
## Problem Description
14+
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.
15+
16+
### Examples
17+
18+
**Example 1:**
19+
![image](https://assets.leetcode.com/uploads/2019/10/15/untitled-diagram-2.jpg)
20+
```
21+
Input: coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
22+
Output: true
23+
```
24+
25+
**Example 2:**
26+
![image](https://assets.leetcode.com/uploads/2019/10/09/untitled-diagram-1.jpg)
27+
```
28+
Input: coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]
29+
Output: false
30+
```
31+
32+
33+
### Constraints
34+
- `2 <= coordinates.length <= 1000`
35+
- `coordinates[i].length == 2`
36+
- `-10^4 <= coordinates[i][0], coordinates[i][1] <= 10^4`
37+
- `coordinates contains no duplicate point.`
38+
39+
## Solution for 1232. Check If It Is a Straight Line Problem
40+
### Approach
41+
1. **Calculate the Initial Slope**:
42+
- Compute the difference in the `y` values (`diff_y`) and the difference in the `x` values (`diff_x`) between the first two points.
43+
44+
2. **Iterate through the Points**:
45+
- For each subsequent point, compute the product of the initial slope differences with the current point's differences.
46+
- If at any point the calculated products do not match, the points do not lie on a straight line.
47+
48+
3. **Return the Result**:
49+
- If all points satisfy the condition, return `true`. Otherwise, return `false`.
50+
51+
<Tabs>
52+
<TabItem value="Solution" label="Solution">
53+
54+
#### Implementation
55+
```jsx live
56+
function Solution(arr) {
57+
function checkStraightLine(coordinates) {
58+
const diff_y = (coordinates[1][1] - coordinates[0][1]);
59+
const diff_x = (coordinates[1][0] - coordinates[0][0]);
60+
for (let i = 2; i < coordinates.length; i++) {
61+
if (diff_y * (coordinates[i][0] - coordinates[i-1][0]) !== diff_x * (coordinates[i][1] - coordinates[i-1][1])) {
62+
return false;
63+
}
64+
}
65+
return true;
66+
}
67+
const input = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
68+
const output = checkStraightLine(input)
69+
return (
70+
<div>
71+
<p>
72+
<b>Input: </b>
73+
{JSON.stringify(input)}
74+
</p>
75+
<p>
76+
<b>Output:</b> {output.toString()}
77+
</p>
78+
</div>
79+
);
80+
}
81+
```
82+
83+
#### Complexity Analysis
84+
85+
- Time Complexity: $ O(N) $
86+
- Space Complexity: $ O(1)$
87+
88+
## Code in Different Languages
89+
<Tabs>
90+
<TabItem value="JavaScript" label="JavaScript">
91+
<SolutionAuthor name="@hiteshgahanolia"/>
92+
```javascript
93+
function checkStraightLine(coordinates) {
94+
const diff_y = (coordinates[1][1] - coordinates[0][1]);
95+
const diff_x = (coordinates[1][0] - coordinates[0][0]);
96+
for (let i = 2; i < coordinates.length; i++) {
97+
if (diff_y * (coordinates[i][0] - coordinates[i-1][0]) !== diff_x * (coordinates[i][1] - coordinates[i-1][1])) {
98+
return false;
99+
}
100+
}
101+
return true;
102+
}
103+
```
104+
105+
</TabItem>
106+
<TabItem value="TypeScript" label="TypeScript">
107+
<SolutionAuthor name="@hiteshgahanolia"/>
108+
```typescript
109+
class Solution {
110+
checkStraightLine(coordinates: number[][]): boolean {
111+
const diff_y = (coordinates[1][1] - coordinates[0][1]);
112+
const diff_x = (coordinates[1][0] - coordinates[0][0]);
113+
for (let i = 2; i < coordinates.length; i++) {
114+
if (diff_y * (coordinates[i][0] - coordinates[i-1][0]) !== diff_x * (coordinates[i][1] - coordinates[i-1][1])) {
115+
return false;
116+
}
117+
}
118+
return true;
119+
}
120+
}
121+
122+
```
123+
</TabItem>
124+
<TabItem value="Python" label="Python">
125+
<SolutionAuthor name="@hiteshgahanolia"/>
126+
```python
127+
class Solution:
128+
def checkStraightLine(self, coordinates: List[List[int]]) -> bool:
129+
diff_y = (coordinates[1][1] - coordinates[0][1])
130+
diff_x = (coordinates[1][0] - coordinates[0][0])
131+
for i in range(2, len(coordinates)):
132+
if diff_y * (coordinates[i][0] - coordinates[i-1][0]) != diff_x * (coordinates[i][1] - coordinates[i-1][1]):
133+
return False
134+
return True
135+
136+
```
137+
138+
</TabItem>
139+
<TabItem value="Java" label="Java">
140+
<SolutionAuthor name="@hiteshgahanolia"/>
141+
```java
142+
143+
import java.util.List;
144+
145+
class Solution {
146+
public boolean checkStraightLine(List<int[]> coordinates) {
147+
int diff_y = (coordinates.get(1)[1] - coordinates.get(0)[1]);
148+
int diff_x = (coordinates.get(1)[0] - coordinates.get(0)[0]);
149+
for (int i = 2; i < coordinates.size(); i++) {
150+
if (diff_y * (coordinates.get(i)[0] - coordinates.get(i-1)[0]) != diff_x * (coordinates.get(i)[1] - coordinates.get(i-1)[1])) {
151+
return false;
152+
}
153+
}
154+
return true;
155+
}
156+
}
157+
158+
```
159+
160+
</TabItem>
161+
<TabItem value="C++" label="C++">
162+
<SolutionAuthor name="@hiteshgahanolia"/>
163+
```cpp
164+
class Solution {
165+
public:
166+
bool checkStraightLine(vector<vector<int>>& coordinates) {
167+
int diff_y = (coordinates[1][1] - coordinates[0][1]);
168+
int diff_x = (coordinates[1][0] - coordinates[0][0]);
169+
for(int i=2; i<coordinates.size(); i++)
170+
{
171+
if(diff_y*(coordinates[i][0] - coordinates[i-1][0]) != diff_x*(coordinates[i][1] - coordinates[i-1][1]))
172+
return false;
173+
}
174+
return true;
175+
}
176+
};
177+
```
178+
</TabItem>
179+
</Tabs>
180+
181+
</TabItem>
182+
</Tabs>
183+
184+
## References
185+
186+
- **LeetCode Problem**: [ Check If It Is a Straight Line](https://leetcode.com/problems/check-if-it-is-a-straight-line/)
187+
188+
- **Solution Link**: [LeetCode Solution](https://leetcode.com/problems/check-if-it-is-a-straight-line/)
189+

0 commit comments

Comments
 (0)