Skip to main content
Back to problems
#447
Medium Algorithms

Number of boomerangs

Array Hash Table Math
57.3% acceptance
Jan 13, 2026
896
1041
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.

Solution

Rust
Time O(n²)
Space O(n)
LeetCode
solution.rs
use std::collections::HashMap;

impl Solution {
  pub fn number_of_boomerangs(points: Vec<Vec<i32>>) -> i32 {
    let mut count = 0;
    
    for i in 0..points.len() {
      let mut map = HashMap::new();
      
      for j in 0..points.len() {
        if i != j {
          let dx = points[i][0] - points[j][0];
          let dy = points[i][1] - points[j][1];
          let dist = dx * dx + dy * dy;
          *map.entry(dist).or_insert(0) += 1;
        }
      }
      
      for &v in map.values() {
        count += v * (v - 1);
      }
    }
    
    count
  }
}