Number of Boomerangs LeetCode Solution

Here, We see Number of Boomerangs LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

List of all LeetCode Solution

Number of Boomerangs LeetCode Solution

Number of Boomerangs LeetCode Solution

Problem Statement

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.

Example 1:
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]].

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

Example 3:
Input: points = [[1,1]]
Output: 0

Number of Boomerangs Solution C++

class Solution {
public:
    int numberOfBoomerangs(vector<vector<int>>& points) {
        int result = 0;
        unordered_map<int, int> umap;
        for(int i=0; i<points.size() ; i++){
            for(int j=0 ; j<points.size() ; j++){
                int d = pow(points[j][1] - points[i][1], 2) + pow(points[j][0] - points[i][0], 2);
                result += 2 * umap[d]++;
            }
            umap.clear();
        }
        return result;
    }
};Code language: PHP (php)

Number of Boomerangs Solution Java

class Solution {
    public int numberOfBoomerangs(int[][] points) {
		if(points == null || points.length == 0 || points[0].length == 0){
			return 0;
		}
		int count = 0;
		Map<Integer,Integer> hashMap = new HashMap<>();
		for (int i = 0;i < points.length;i++){
			hashMap.clear();
			for (int j = 0;j < points.length;j++){
				if (i == j){
					continue;
				}
				int distance = (points[j][0]-points[i][0])*(points[j][0]-points[i][0]) + (points[j][1]-points[i][1]) * (points[j][1]-points[i][1]);
				count += hashMap.getOrDefault(distance,0) * 2;
				hashMap.put(distance,hashMap.getOrDefault(distance,0) + 1);
			}
		}
		return count;
	}
}Code language: JavaScript (javascript)

Number of Boomerangs Solution JavaScript

var numberOfBoomerangs = function(points) {
    let count = 0;
    for (let i = 0; i < points.length; i++) {
        const memory = {};
        for (let j = 0; j < points.length; j++) {
            if (i === j) continue;
            const dist = Math.pow(points[i][0] - points[j][0],2) + Math.pow(points[i][1] - points[j][1],2);
            if (memory[dist]) count += memory[dist] * 2;
            memory[dist] ? memory[dist] += 1 : memory[dist] = 1;
        }
    }
    return count;
};Code language: JavaScript (javascript)

Number of Boomerangs Solution Python

class Solution(object):
    def numberOfBoomerangs(self, points):
        n = 0
        for a,b in points:
            counter = {}
            for x,y in points:
                key = (x-a)**2 + (y-b)**2
                if key in counter:
                    n += 2*counter[key]
                    counter[key] += 1
                else:
                    counter[key] = 1
        return n
Scroll to Top