Number of Boomerangs - Problem
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.
Input & Output
Example 1 — Basic Boomerangs
$
Input:
points = [[0,0],[1,0],[2,0]]
›
Output:
2
💡 Note:
Two boomerangs exist: (1,0,2) and (1,2,0). From point [1,0]: distance to [0,0] = 1, distance to [2,0] = 1 (equal distances). The other points don't have equal distances to any pair of other points.
Example 2 — Square Formation
$
Input:
points = [[1,1],[2,2],[3,3]]
›
Output:
2
💡 Note:
Points form a diagonal line. From [2,2]: distance to [1,1] = √2, distance to [3,3] = √2. So (2,1,3) and (2,3,1) are valid boomerangs.
Example 3 — No Boomerangs
$
Input:
points = [[1,1]]
›
Output:
0
💡 Note:
Only one point exists, so no boomerangs can be formed (need at least 3 points).
Constraints
- n == points.length
- 1 ≤ n ≤ 500
- points[i].length == 2
- -104 ≤ xi, yi ≤ 104
- All the points are unique
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code