You are given n points in the plane that are all distinct, where points[i] = [xi, yi]. A boomerang is a tuple of points (i, j, k) such that the distance between i and j equals the distance between i and k (the order of the tuple matters).
Return the number of boomerangs.
Example 1:
Input: points = [[0,0],[1,0],[2,0]] Output: 2 Explanation: The two boomerangs are [[1,0],[0,0],[2,0]] and [[1,0],[2,0],[0,0]].
Example 2:
Input: points = [[1,1],[2,2],[3,3]] Output: 2
Example 3:
Input: points = [[1,1]] Output: 0
Constraints:
n == points.length1 <= n <= 500points[i].length == 2-104 <= xi, yi <= 104When 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:
The brute force approach to finding boomerangs involves examining every possible trio of points. We'll check each trio to see if they form a valid boomerang pattern based on distance. We'll count the number of trios that meet our criteria.
Here's how the algorithm would work step-by-step:
def number_of_boomerangs_brute_force(points):
number_of_boomerangs = 0
for index_first_point in range(len(points)):
for index_second_point in range(len(points)):
# Ensure second point is different from the first.
if index_first_point == index_second_point:
continue
for index_third_point in range(len(points)):
# Ensure third point is different from the first and second.
if index_first_point == index_third_point or index_second_point == index_third_point:
continue
point_a = points[index_first_point]
point_b = points[index_second_point]
point_c = points[index_third_point]
distance_ab = (point_a[0] - point_b[0])**2 + (point_a[1] - point_b[1])**2
distance_ac = (point_a[0] - point_c[0])**2 + (point_a[1] - point_c[1])**2
# Check if the distances from first to second and first to third are equal.
if distance_ab == distance_ac:
number_of_boomerangs += 1
return number_of_boomerangsThe key is to focus on each point and count how many other points are the same distance away from it. Then, for each point, use the count of points at the same distance to quickly determine the number of boomerangs that can be formed with that point as the center, without checking all possible combinations.
Here's how the algorithm would work step-by-step:
def number_of_boomerangs(points):
number_of_boomerangs_found = 0
for point_index in range(len(points)):
distances = {}
# Iterate through all other points to calculate distances.
for other_point_index in range(len(points)):
if point_index != other_point_index:
point_x, point_y = points[point_index]
other_point_x, other_point_y = points[other_point_index]
distance = (point_x - other_point_x)**2 + (point_y - other_point_y)**2
if distance not in distances:
distances[distance] = 0
distances[distance] += 1
# Calculate boomerangs based on point counts at each distance.
for distance_value in distances:
number_of_points_at_distance = distances[distance_value]
# Boomerang count: n * (n - 1) permutations
number_of_boomerangs_found += number_of_points_at_distance * (number_of_points_at_distance - 1)
return number_of_boomerangs_found| Case | How to Handle |
|---|---|
| Null or empty input array | Return 0 immediately as no boomerangs can be formed. |
| Array with less than 3 points | Return 0 as at least 3 points are required to form a boomerang. |
| All points are the same | The distance between each pair will be 0, leading to n*(n-1) boomerangs from that single point which can cause integer overflow; use long for distances to avoid overflow. |
| Integer overflow in distance calculation (x*x + y*y) | Use long data type to store the square of the differences to avoid integer overflow during distance calculation. |
| Large input array (performance consideration) | The O(n^2) solution may become slow; optimize distance calculation if possible or consider more efficient data structures if available with further problem constraints. |
| Points with extremely large coordinate values | Using a long datatype for intermediate calculations will prevent potential overflow with very large squared differences. |
| Points are collinear (lying on the same line) | The hash map correctly counts distances even when points are collinear. |
| Duplicate points in the array | Duplicate points will be treated as distinct points when calculating distances. |