Count Ordered Equal-Distance Point Triples

Quick Overview

Count ordered triples of distinct plane points with equal distances from their center. Practice exact distance comparison, ordering, boundary cases, and a complete written solution.

Count Ordered Equal-Distance Point Triples

Company: Bloomberg

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

# Count Ordered Equal-Distance Point Triples You are given distinct points in the plane. Count ordered triples of distinct point indices `(i,j,k)` for which the distance from point `i` to point `j` equals the distance from point `i` to point `k`. The first point is the center of the comparison. Swapping `j` and `k` produces a different triple. ### Function `int numberOfBoomerangs(vector<vector<int>>& points)` ### Example 1 ```text Input: points = [[0,0],[2,0],[2,2],[0,2]] Output: 8 ``` At each square corner, the two adjacent corners have equal distance from it. Their two possible orders contribute two triples per center, for eight total. ### Example 2 ```text Input: points = [[0,0],[1,0],[0,2]] Output: 0 ``` The three squared side lengths are `1`, `4`, and `5`. No point has two other points at equal distance. ### Constraints - `n == points.length` and `1 <= n <= 500`. - Every point contains exactly two coordinates. - Each coordinate is between `-10000` and `10000`, inclusive. - All points are distinct. ### Clarifications Collinear triples are allowed; the condition concerns equal distances, not an angle or an enclosed area. The three indices must be different. A single point or two points cannot form a valid triple. ### What a Strong Answer Covers - The center's role and the two orders of the remaining points. - Multiple points at the same distance and points at different distances. - Exact distance comparison and numeric safety. - Why the count is complete without counting a triple twice.

Overview: Count ordered triples of distinct plane points with equal distances from their center. Practice exact distance comparison, ordering, boundary cases, and a complete written solution.

Bloomberg logo
Bloomberg
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Count Ordered Equal-Distance Point Triples

You are given distinct points in the plane. Count ordered triples of distinct point indices (i,j,k) for which the distance from point i to point j equals the distance from point i to point k.

The first point is the center of the comparison. Swapping j and k produces a different triple.

Function

int numberOfBoomerangs(vector<vector<int>>& points)

Example 1

Input: points = [[0,0],[2,0],[2,2],[0,2]]
Output: 8

At each square corner, the two adjacent corners have equal distance from it. Their two possible orders contribute two triples per center, for eight total.

Example 2

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

The three squared side lengths are 1, 4, and 5. No point has two other points at equal distance.

Constraints

  • n == points.length and 1 <= n <= 500 .
  • Every point contains exactly two coordinates.
  • Each coordinate is between -10000 and 10000 , inclusive.
  • All points are distinct.

Clarifications

Collinear triples are allowed; the condition concerns equal distances, not an angle or an enclosed area. The three indices must be different. A single point or two points cannot form a valid triple.

What a Strong Answer Covers Guidance

  • The center's role and the two orders of the remaining points.
  • Multiple points at the same distance and points at different distances.
  • Exact distance comparison and numeric safety.
  • Why the count is complete without counting a triple twice.