Given an array points where points[i] = [xi, yi] represents a point on the X-Y plane, return true if these points are a boomerang.
A boomerang is a set of three points that are all distinct and not in a straight line.
Example 1:
Input: points = [[1,1],[2,3],[3,2]] Output: true
Example 2:
Input: points = [[1,1],[2,2],[3,3]] Output: false
Constraints:
points.length == 3points[i].length == 20 <= xi, yi <= 100When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
A boomerang is formed by three points. To check if these three points form a valid boomerang, we can directly examine all the possible combinations of these points and verify whether they meet the required conditions. This approach involves comparing all possibilities to identify a valid boomerang.
Here's how the algorithm would work step-by-step:
def is_boomerang(points):
point_a, point_b, point_c = points
# If any two points are the same, it's not a boomerang.
if point_a == point_b or point_a == point_c or point_b == point_c:
return False
# Check if the three points are on the same line.
# This avoids division by zero when calculating slope
if (point_b[1] - point_a[1]) * (point_c[0] - point_b[0]) == \
(point_c[1] - point_b[1]) * (point_b[0] - point_a[0]):
return False
# If the points are distinct and not collinear, it's a boomerang.
return TrueThe fastest way to determine if three points form a valid boomerang is to check if they all lie on the same line. This involves checking if the area formed by these points is zero, meaning they are collinear. If they are collinear, they do not form a boomerang.
Here's how the algorithm would work step-by-step:
def is_boomerang(points):
point_one = points[0]
point_two = points[1]
point_three = points[2]
# Need three distinct points to form a boomerang.
if point_one == point_two or point_one == point_three or point_two == point_three:
return False
# Calculate the area of the triangle formed.
area = (point_one[0] * (point_two[1] - point_three[1]) +
point_two[0] * (point_three[1] - point_one[1]) +
point_three[0] * (point_one[1] - point_two[1]))
# A zero area indicates collinearity; not a boomerang.
if area == 0:
return False
return True| Case | How to Handle |
|---|---|
| Input is null or any of the point arrays is null | Throw an IllegalArgumentException or return false since null inputs are invalid. |
| Any two points are identical | Return false since a boomerang must have distinct points. |
| All three points are collinear (lie on the same line) | Return false because collinear points do not form a boomerang, which requires a non-zero area. |
| Points have extremely large or small coordinate values (potential integer overflow) | Use long data type for calculations to prevent integer overflow during area computation. |
| Points are very close together, leading to potential floating point precision issues | Accept a small tolerance for collinearity using a very small epsilon value during area calculation comparison to zero. |
| Input points array contains more or less than 3 elements | Throw an IllegalArgumentException or return false because the input is invalid. |
| One or more points has identical x and y coordinates (e.g. [0, 0], [0, 0], [1, 1]) | The collinearity check will correctly handle such cases, returning false. |
| Points form a very thin, almost-collinear triangle | The area calculation, with a tolerance value, should still correctly determine if it's considered a valid boomerang. |