LeetCode Problem

How to Solve Count Number of Trapezoids I

To solve this problem, efficiently scan the list of points and use a hash table to count points sharing the same y-coordinate. Check all possible combinations of points forming horizontal trapezoids by comparing the y-coordinates of selected points. Analyzing the problem in terms of geometric properties, such as parallel lines, simplifies the approach significantly.

GhostInterview Help

Need help with Count Number of Trapezoids I without spending extra time grinding it?

GhostInterview can read Count Number of Trapezoids I from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #3623Array scanning plus hash lookupReviewed 2026-03-08
Difficulty
Medium
Primary pattern
Array scanning plus hash lookup
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

To solve this problem, efficiently scan the list of points and use a hash table to count points sharing the same y-coordinate. Check all possible combinations of points forming horizontal trapezoids by comparing the y-coordinates of selected points. Analyzing the problem in terms of geometric properties, such as parallel lines, simplifies the approach significantly.

Problem Statement

You are given a 2D integer array points, where each point is represented as [xi, yi] on the Cartesian plane. A horizontal trapezoid is a convex quadrilateral that has at least one pair of sides parallel to the x-axis. This means that the selected points must have the same y-coordinate for the parallel sides.

Your task is to return the number of distinct horizontal trapezoids that can be formed by selecting four points from the array, with the constraint that all points are pairwise distinct.

Examples

Example 1

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

Output: 3

There are three distinct ways to pick four points that form a horizontal trapezoid:

Example 2

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

Output: 1

There is only one horizontal trapezoid that can be formed.

Constraints

  • 4 <= points.length <= 105
  • –108 <= xi, yi <= 108
  • All points are pairwise distinct.

Solution Approach

Scan the points and categorize by y-coordinate

To efficiently count horizontal trapezoids, scan through the array of points, grouping the points by their y-coordinate. This allows easy identification of potential pairs of parallel lines.

Use hash table to track frequency

Implement a hash table to store the frequency of each y-coordinate. By doing so, it becomes easier to calculate how many points lie on a line parallel to the x-axis, which helps to form the parallel sides of the trapezoid.

Count valid trapezoids based on conditions

Once the points are categorized and stored, count the number of ways to select four points with the same y-coordinate to form the two horizontal sides of a trapezoid. Carefully check the geometric conditions for a valid trapezoid formation.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

The time complexity depends on the hash table operations, which in the worst case are O(n) for scanning the points and O(1) for each insertion or lookup. The space complexity is O(n) due to the need to store the points and hash table entries.

What Interviewers Usually Probe

  • Candidate demonstrates understanding of geometric properties like parallel lines.
  • Candidate efficiently uses hash tables to track and group points.
  • Candidate is able to optimize their approach to handle the input size constraint.

Common Pitfalls or Variants

Common pitfalls

  • Misunderstanding the concept of parallel lines, leading to incorrect trapezoid formation.
  • Failure to account for all distinct point combinations.
  • Not optimizing the approach to handle larger inputs within time limits.

Follow-up variants

  • Implementing the solution in a higher-dimensional space.
  • Extending the solution to count trapezoids with vertical sides as well.
  • Exploring alternative approaches like sorting or dynamic programming.

How GhostInterview Helps

  • GhostInterview's problem-specific solver assists in identifying efficient approaches to handle large input sizes.
  • Helps in leveraging geometric properties, focusing on line parallelism and y-coordinate grouping.
  • The tool guides users to avoid common pitfalls and suggests optimal use of hash tables for fast lookups.

Topic Pages

FAQ

How do I count the number of horizontal trapezoids efficiently?

Efficiently count trapezoids by scanning through the points and grouping them by their y-coordinate using a hash table. Then, calculate the possible trapezoids from those groups.

What is the primary pattern in this problem?

The primary pattern is array scanning plus hash lookup, which helps categorize points by their y-coordinate to identify potential trapezoids.

Can the approach handle large input sizes?

Yes, the approach is designed to handle inputs up to 10^5 points efficiently by utilizing hash tables for fast lookups.

What role does the y-coordinate play in forming a trapezoid?

The y-coordinate determines which points can form the horizontal sides of the trapezoid, as these sides must be parallel to the x-axis.

How do I optimize the solution for this problem?

Optimize by categorizing points by y-coordinate using a hash table, which enables fast lookups and efficient counting of potential trapezoids.

GhostInterview Solver

Need direct help with Count Number of Trapezoids I instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Count Number of Trapezoids I from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.