tutorialcup
Valid Boomerang Leetcode Solution
Problem Statement
In this problem, we are given a set of three points in an X-Y 2-D plane. We need to return whether they form a boomerang or not, that is whether they are any three distinct points and do not form a straight line.
Example
Points = {{1 , 2} , {2 , 6} , {1 , 2}}
false
Points = {{1 , 1} , {2 , 3} , {6 , 7}}
true
The first input has two same points out of 3, so it is not a valid boomerang and we print false. The second test has 3 distinct points that do not form a straight line and we print true.
Approach(Slope Test)
In the problem Check If It is a Straight Line, we have learnt that three distinct points are only collinear if the slope of the line formed by every pair of points is the same. Here, we need to check:
- If points are distinct
- the points do not lie on a straight line
If any pair of points is the same, then the given input will pass the collinearity test, as any 2 points(or a single point) are always collinear. So, we just need to check for the equality of slopes. Note that if any three points, P1, P2 and P3 are collinear, we have
(y2 - y1) : (x2 - x1) :: (y3 - y2) : (x3 - x2) , or
(y2 - y1) * (x3 - x2) = (x2 - x1) * (y3 - y2)
where x1, x2, x3, y1, y2, y3 are the corresponding x and t coordinates of P1, P2 and P3.
Algorithm
www.tutorialcup.com/leetcode-solutions/valid-boomerang-le...
Valid Boomerang Leetcode Solution
Problem Statement
In this problem, we are given a set of three points in an X-Y 2-D plane. We need to return whether they form a boomerang or not, that is whether they are any three distinct points and do not form a straight line.
Example
Points = {{1 , 2} , {2 , 6} , {1 , 2}}
false
Points = {{1 , 1} , {2 , 3} , {6 , 7}}
true
The first input has two same points out of 3, so it is not a valid boomerang and we print false. The second test has 3 distinct points that do not form a straight line and we print true.
Approach(Slope Test)
In the problem Check If It is a Straight Line, we have learnt that three distinct points are only collinear if the slope of the line formed by every pair of points is the same. Here, we need to check:
- If points are distinct
- the points do not lie on a straight line
If any pair of points is the same, then the given input will pass the collinearity test, as any 2 points(or a single point) are always collinear. So, we just need to check for the equality of slopes. Note that if any three points, P1, P2 and P3 are collinear, we have
(y2 - y1) : (x2 - x1) :: (y3 - y2) : (x3 - x2) , or
(y2 - y1) * (x3 - x2) = (x2 - x1) * (y3 - y2)
where x1, x2, x3, y1, y2, y3 are the corresponding x and t coordinates of P1, P2 and P3.
Algorithm
www.tutorialcup.com/leetcode-solutions/valid-boomerang-le...