# Number of Boomerangs – LeetCode Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.

You are given `n`

`points`

in the plane that are all **distinct**, where `points[i] = [x`

. A _{i}, y_{i}]**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:2Explanation: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.length`

`1 <= n <= 500`

`points[i].length == 2`

`-10`

^{4}<= x_{i}, y_{i}<= 10^{4}- All the points are
**unique**.

# C++ Number of Boomerangs LeetCode Solution

```
``````
int numberOfBoomerangs(vector<pair<int, int>>& points) {
int res = 0;
// iterate over all the points
for (int i = 0; i < points.size(); ++i) {
unordered_map<long, int> group(points.size());
// iterate over all points other than points[i]
for (int j = 0; j < points.size(); ++j) {
if (j == i) continue;
int dy = points[i].second - points[j].second;
int dx = points[i].first - points[j].first;
// compute squared euclidean distance from points[i]
int key = dy * dy;
key += dx * dx;
// accumulate # of such "j"s that are "key" distance from "i"
++group[key];
}
for (auto& p : group) {
if (p.second > 1) {
/*
* for all the groups of points,
* number of ways to select 2 from n =
* nP2 = n!/(n - 2)! = n * (n - 1)
*/
res += p.second * (p.second - 1);
}
}
}
return res;
}
```

# Java Number of Boomerangs LeetCode Solution

```
``````
public int numberOfBoomerangs(int[][] points) {
int res = 0;
Map<Integer, Integer> map = new HashMap<>();
for(int i=0; i<points.length; i++) {
for(int j=0; j<points.length; j++) {
if(i == j)
continue;
int d = getDistance(points[i], points[j]);
map.put(d, map.getOrDefault(d, 0) + 1);
}
for(int val : map.values()) {
res += val * (val-1);
}
map.clear();
}
return res;
}
private int getDistance(int[] a, int[] b) {
int dx = a[0] - b[0];
int dy = a[1] - b[1];
return dx*dx + dy*dy;
}
```

# Python 3 Number of Boomerangs LeetCode Solution

```
``````
res = 0
for p in points:
cmap = {}
for q in points:
f = p[0]-q[0]
s = p[1]-q[1]
cmap[f*f + s*s] = 1 + cmap.get(f*f + s*s, 0)
for k in cmap:
res += cmap[k] * (cmap[k] -1)
return res
```

Array-1180

String-562

Hash Table-412

Dynamic Programming-390

Math-368

Sorting-264

Greedy-257

Depth-First Search-256

Database-215

Breadth-First Search-200

Tree-195

Binary Search-191

Matrix-176

Binary Tree-160

Two Pointers-151

Bit Manipulation-140

Stack-133

Heap (Priority Queue)-117

Design-116

Graph-108

Simulation-103

Prefix Sum-96

Backtracking-92

Counting-86

Sliding Window-73

Linked List-69

Union Find-66

Ordered Set-48

Monotonic Stack-47

Recursion-43

Trie-41

Binary Search Tree-40

Divide and Conquer-40

Enumeration-39

Bitmask-37

Queue-33

Memoization-32

Topological Sort-31

Geometry-30

Segment Tree-27

Game Theory-24

Hash Function-24

Binary Indexed Tree-21

Interactive-18

Data Stream-17

String Matching-17

Rolling Hash-17

Shortest Path-16

Number Theory-16

Combinatorics-15

Randomized-12

Monotonic Queue-9

Iterator-9

Merge Sort-9

Concurrency-9

Doubly-Linked List-8

Brainteaser-8

Probability and Statistics-7

Quickselect-7

Bucket Sort-6

Suffix Array-6

Minimum Spanning Tree-5

Counting Sort-5

Shell-4

Line Sweep-4

Reservoir Sampling-4

Eulerian Circuit-3

Radix Sort-3

Strongly Connected Componen-t2

Rejection Sampling-2

Biconnected Component-1