Skip to main content
Back to problems
Leetcode
Medium
Arrays
Matrices
Math
Combinatorics
Google
Right Triangles

Count the number of right triangles that can be formed from points on a grid or in an array-based coordinate set.

Acceptance 0%
Problem Statement

Given a set of points in the plane, count how many distinct triangles formed by three points are right triangles.

A triangle is considered a right triangle if one of its angles is exactly 9090^\circ.

Your task is to return the total number of such triangles that can be formed using the given points. Each valid triangle should be counted once, regardless of the order in which its vertices are chosen.

Input Format

  • A collection of points, typically represented as integer coordinate pairs.
  • Each point is given as (x, y).
  • The number of points is the main input size parameter.

Output Format

  • Return a single integer: the number of distinct right triangles that can be formed.

Constraints

  • Points use integer coordinates.
  • A triangle must use three distinct points.
  • Degenerate triples that do not form a valid triangle are excluded.
  • Exact size limits are not specified in the provided metadata; assume a standard interview-style counting problem.
Examples
Sample cases returned by the problem API.

Example 1

Input

points = [[0,0],[1,0],[0,1],[2,0],[0,2]]

Output

6

Explanation

Right triangles can be formed by choosing a point on the x-axis and a point on the y-axis with the origin as the right-angle vertex, plus the larger axis-aligned variants. The distinct right triangles are counted once each.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.