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) because distance from [1,0] to [0,0] equals distance from [1,0] to [2,0] (both = 1), and (0,1,2) because distance from [0,0] to [1,0] equals distance from [0,0] to [2,0] (both = 1). Wait, let me recalculate: From [1,0]: dist to [0,0] = 1, dist to [2,0] = 1, so (1,0,2) is valid. From [0,0]: dist to [1,0] = 1, dist to [2,0] = 2, not equal. From [2,0]: dist to [0,0] = 2, dist to [1,0] = 1, not equal. Actually, only 2 boomerangs: (1,0,2) and (1,2,0).
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
Boomerangs: Points with Equal Distances from CenterCenterABCDDistance to A = Distance to B (horizontal pair)Distance to C = Distance to D (vertical pair)Boomerangs: (Center,A,B), (Center,B,A), (Center,C,D), (Center,D,C)Result: 4 boomerangs found
Understanding the Visualization
1
Input Points
Given array of coordinate points
2
Find Equal Distances
For each center point, find pairs at equal distances
3
Count Boomerangs
Sum all valid boomerang combinations
Key Takeaway
🎯 Key Insight: Use hash map to group points by distance from each center
Asked in
Google 15 Amazon 12 Apple 8
23.4K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen