447. Number of Boomerangs
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to find the number of "boomerangs" from a given list of distinct points in a 2D plane. A boomerang is defined as a tuple of points (i, j, k) where the distance between point i and point j is equal to the distance between point i and point k. The problem emphasizes that the order of the tuple matters, meaning (i, j, k) is different from (i, k, j) if j and k are different points. Point i acts as the pivot, and points j and k are equidistant from it. For a valid boomerang with three distinct points, i, j, and k must all be different.
Solution
Number of Boomerangs in Python
class Solution:
def numberOfBoomerangs(self, points: list[list[int]]) -> int:
n = len(points)
if n < 3:
return 0 # Not enough points to form a boomerang.
total_boomerangs = 0
# Iterate through each point and consider it as the pivot 'i'.
for i in range(n):
# A hash map to store squared distances from the current pivot 'i'
# to all other points 'j'. Key: squared distance, Value: count of points.
dist_counts = {}
# Iterate through all other points 'j' to calculate distances from 'i'.
for j in range(n):
if i == j:
continue
# Calculate squared Euclidean distance to avoid floating point issues and sqrt.
# Python's integers handle arbitrary size, so no overflow issues.
dx = points[i][0] - points[j][0]
dy = points[i][1] - points[j][1]
dist_sq = dx*dx + dy*dy
dist_counts[dist_sq] = dist_counts.get(dist_sq, 0) + 1
# For each distance, if 'm' points are found, they can form m * (m-1) boomerangs.
for count in dist_counts.values():
total_boomerangs += count * (count - 1)
return total_boomerangs
Number of Boomerangs in Java
class Solution {
public int numberOfBoomerangs(int[][] points) {
int n = points.length;
if (n < 3) {
return 0; // Not enough points to form a boomerang.
}
int totalBoomerangs = 0;
// Iterate through each point and consider it as the pivot 'i'.
for (int i = 0; i < n; i++) {
// A hash map to store squared distances from the current pivot 'i'
// to all other points 'j'. Key: squared distance, Value: count of points.
Map<Long, Integer> distCounts = new HashMap<>();
// Iterate through all other points 'j' to calculate distances from 'i'.
for (int j = 0; j < n; j++) {
if (i == j) {
continue;
}
// Calculate squared Euclidean distance to avoid floating point issues and sqrt.
// Use 'long' for dx, dy, and distSq to prevent overflow, as coordinates can be up to 10^4.
long dx = (long)points[i][0] - points[j][0];
long dy = (long)points[i][1] - points[j][1];
long distSq = dx * dx + dy * dy;
distCounts.put(distSq, distCounts.getOrDefault(distSq, 0) + 1);
}
// For each distance, if 'm' points are found, they can form m * (m-1) boomerangs.
for (int count : distCounts.values()) {
totalBoomerangs += count * (count - 1);
}
}
return totalBoomerangs;
}
}
Number of Boomerangs in C++
class Solution {
public:
int numberOfBoomerangs(vector<vector<int>>& points) {
int n = points.size();
if (n < 3) {
return 0; // Not enough points to form a boomerang.
}
int total_boomerangs = 0;
// Iterate through each point and consider it as the pivot 'i'.
for (int i = 0; i < n; ++i) {
// A hash map to store squared distances from the current pivot 'i'
// to all other points 'j'. Key: squared distance, Value: count of points.
unordered_map<long, int> dist_counts;
// Iterate through all other points 'j' to calculate distances from 'i'.
for (int j = 0; j < n; ++j) {
if (i == j) {
continue;
}
// Calculate squared Euclidean distance to avoid floating point issues and sqrt.
// Use 'long' for dx, dy, and dist_sq to prevent overflow, as coordinates can be up to 10^4.
long dx = (long)points[i][0] - points[j][0];
long dy = (long)points[i][1] - points[j][1];
long dist_sq = dx * dx + dy * dy;
dist_counts[dist_sq]++;
}
// For each distance, if 'm' points are found, they can form m * (m-1) boomerangs.
for (auto const& [dist, count] : dist_counts) {
total_boomerangs += count * (count - 1);
}
}
return total_boomerangs;
}
};
Number of Boomerangs in TypeScript
function numberOfBoomerangs(points: number[][]): number {
const n = points.length;
if (n < 3) {
return 0; // Not enough points to form a boomerang.
}
let totalBoomerangs = 0;
// Iterate through each point and consider it as the pivot 'i'.
for (let i = 0; i < n; i++) {
// A map to store squared distances from the current pivot 'i'
// to all other points 'j'. Key: squared distance, Value: count of points.
const distCounts: Map<number, number> = new Map();
// Iterate through all other points 'j' to calculate distances from 'i'.
for (let j = 0; j < n; j++) {
if (i === j) {
continue;
}
// Calculate squared Euclidean distance to avoid floating point issues and sqrt.
// JavaScript's 'number' type is a 64-bit float, which can safely store the squared distance.
const dx = points[i][0] - points[j][0];
const dy = points[i][1] - points[j][1];
const distSq = dx * dx + dy * dy;
distCounts.set(distSq, (distCounts.get(distSq) || 0) + 1);
}
// For each distance, if 'm' points are found, they can form m * (m-1) boomerangs.
for (const count of distCounts.values()) {
totalBoomerangs += count * (count - 1);
}
}
return totalBoomerangs;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.