> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Number of Boomerangs Python Solution

> Tested Python solution for LeetCode 447 with 24 pytest cases. Generate a practice environment with lcpy.

LeetCode 447, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Math](/catalog/topics/math). [View on LeetCode](https://leetcode.com/problems/number-of-boomerangs/description/).

Generate this problem as a practice environment: tested reference solution, 24 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 447   # by problem number
lcpy gen -s number_of_boomerangs   # by problem name
```

## 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).

### Examples

```
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]].
```

```
Input: points = [[1,1],[2,2],[3,3]]
Output: 2
```

```
Input: points = [[1,1]]
Output: 0
```

### Constraints

* 1 \<= n \<= 500
* points\[i].length == 2
* -10^4 \<= xi, yi \<= 10^4
* All the points are unique.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/number_of_boomerangs/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/number_of_boomerangs/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import defaultdict


class Solution:
    # Time: O(n^2)
    # Space: O(n)
    def number_of_boomerangs(self, points: list[list[int]]) -> int:
        total = 0
        for i, (xi, yi) in enumerate(points):
            dist_counts: dict[int, int] = defaultdict(int)
            for j, (xj, yj) in enumerate(points):
                if j == i:
                    continue
                dist_counts[(xj - xi) ** 2 + (yj - yi) ** 2] += 1
            total += sum(count * (count - 1) for count in dist_counts.values())
        return total
```

## Complexity

| Time | Space |
| - | - |
| O(n^2) | O(n) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.