Check If It Is a Straight Line | Leetcode #1232 | Find if all coordinate points form a straight line

preview_player
Показать описание
This video explains a very interesting geometrical problem which is to find if the given coordinate points forms a straight line. This can be solved using the slope comparison between any two given points. If slopes are equal then they are in the same straight line otherwise not. I have shown a modified approach for using the slope formula which will eliminate division by zero problem. At the end of the video, i have explained the CODE for this algorithm. CODE LINK is present below as usual. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)

SIMILAR PROBLEMS:-
Рекомендации по теме
Комментарии
Автор

Changing the division to multiplication was smart. I forgot about that.

The multiplication works, because we do not care about the actual result. We only care if the result is same. So we are not even calculatiing the slope anymore, we are calculating its "invert". But since we only use that value to compare and not to actually define the slope, its fine.

Smart!

cloud
Автор

Other alternative can be we can include a if condition saying if x2-x1 ==0 and y2-y1==0 then we continue to the next iteration?

tejaswigutta
Автор

If four points have same slope, then still make 2 parallel lines. i.e. not on same straight line

sainirj
Автор

Using absolute in finding differences will not pass one test case because.Btw Thanks for the multiplication thing to handle Infinite slope.
remove the abs from everywhere in this code and it will pass all the test cases.

shivamraj
Автор

Very nicely explained ! Please discuss more interesting problems like these if you can !Thanks

akhila
Автор

class Solution {
public:
bool A) {
for(int i=1;i<A.size()-1;i++){
double y2y1=A[i][1]-A[i-1][1], x2x1=A[i][0]-A[i-1][0];
double y3y2=A[i+1][1]-A[i][1], x3x2=A[i+1][0]-A[i][0];
if(y3y2*x2x1!=y2y1*x3x2) return false;
}
return true;
}
};

mutyamreddybejjanki
Автор

How did you get this idea to use that formula when duplicate co ordinates are present ? I would surely not get such an idea if I would be given this question in an interview :(

tejaswigutta
Автор

Sir new test cases have been added on leetcode, getting wrong answer :-
Input
[[1, -8], [2, -3], [1, 2]]
Output
true
Expected
false

ABHISHEK-fyin
Автор

Python3 solution:


def

if len(list_of_points)==2:
return True
else:
i = 2
while i<len(list_of_points):

x1 = list_of_points[i-2][0]
y1 = list_of_points[i-2][1]

x2 = list_of_points[i-1][0]
y2 = list_of_points[i-1][1]

x3 = list_of_points[i][0]
y3 = list_of_points[i][1]

if (y2-y1)*(x3-x2) != (y3-y2)*(x2-x1):
return False

i += 1

return True






if __name__ == '__main__':
number_of_points = int(input('Enter number of points between 2 and 1000: '))

list_of_points = []
for i in range(0, number_of_points):
input_point = tuple(map(int, input().split(', ')))


boolean_value =

if boolean_value:
print('Points form a straight line.')
else:
print('Not a straight line!')

anujvyas
Автор

Thank u very much sir.. Hope you make more videos like this...

gajatejeswarareddy
Автор

Other way to solve this problem is that we can write equation of line with the help of two points and the other points which satisfy this will be on the straight line.

ayanraj
Автор

Hey, I found this helpful. Thanks
Although I can't find the line equation made by joining these points, if you know please do share. Although it's fine
Thanks for the video

kapil
Автор

We have a formula for Collinear points in analytical geometry....will that work

If area is equals 0....then they are Collinear

bharadwajachepuri
Автор

Go ahead sir and request to make a video tutorial on Linked List.

sofiullahiqbalkiron
Автор

I love your channel. Where di you learn CP from. I don't see people using tie_with_stdio_ and all? Is there someplace I can learn all this from?


Thanks again for the content. I love it.

EinstienJr
Автор

Simple logic different between two point must be one

shanmukhavarma
Автор

How about Imaginary Number ( i^2= -1 ) ? ? The Imaginary Points Coordinates that it will return False But how would we handle those Coordinates Slope?? Thanks Its a lot Very Helpful video

techgamer
Автор

I don't see if this algorithm will pass all the cases... that absolute difference you do is not necessary i think because between slopes "-m" and "m" there is a difference of 90°
May you please clearify my doubt?
And thankyou for that multiplication thing...

kaisarshabir
Автор

Sir if you don't mind could you do a video on Steps by Knight? please sir

Chandutravelvlogs
Автор

If x2-x1 is 0 then we can divide by 1 else divide by x2-x1 then also we can get answer

retrogame