The fastest approach combines scanning all point pairs and hashing each pair's slope in reduced form. By normalizing slopes using GCD and consistent sign handling, you can detect parallel sides reliably. Then, counting combinations of pairs with matching slopes yields the total number of trapezoids efficiently without redundant checks.
Problem Statement
Given a list of 2D points, each represented as [x, y], determine how many unique trapezoids can be formed using exactly four distinct points. A trapezoid is defined as a convex quadrilateral with at least one pair of sides parallel.
Two sides are parallel if their slopes are equal. Return the total count of all possible trapezoids from the given points list. Points are guaranteed to be distinct, and coordinates range between -1000 and 1000.
Examples
Example 1
Input: points = [[-3,2],[3,0],[2,3],[3,2],[2,-3]]
Output: 2
There are two distinct ways to pick four points that form a trapezoid:
Example 2
Input: points = [[0,0],[1,0],[0,1],[2,1]]
Output: 1
There is only one trapezoid which can be formed.
Constraints
- 4 <= points.length <= 500
- –1000 <= xi, yi <= 1000
- All points are pairwise distinct.
Solution Approach
Normalize slopes for all point pairs
Iterate over all unique pairs of points and compute the slope as a fraction reduced by GCD. Fix the sign to keep consistency. Store each pair in a hash map keyed by its slope to quickly locate parallel candidates.
Combine parallel pairs
For each slope group, consider combinations of two pairs that do not share points. Each valid combination corresponds to one trapezoid. This step leverages array scanning and hash lookup to avoid O(n^4) brute force.
Count total trapezoids
Accumulate all valid trapezoid counts from the slope groups. Ensure no duplicates by only pairing disjoint point pairs. Return the final count as the answer.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
Time complexity depends on generating all point pairs O(n^2) and hashing slopes, then combining pairs which is manageable with hash map groups. Space complexity is O(n^2) for storing slope maps.
What Interviewers Usually Probe
- Look for a method to normalize slopes instead of comparing floating points.
- Notice that brute-force four-point iteration will exceed time limits for n=500.
- Expect hash-based grouping of slopes as the primary optimization.
Common Pitfalls or Variants
Common pitfalls
- Failing to reduce slope fractions correctly leads to missed parallel pairs.
- Including overlapping point pairs in the same trapezoid counts duplicates.
- Using floating-point slope comparisons without normalization causes precision errors.
Follow-up variants
- Count trapezoids in 3D by projecting onto planes and hashing direction vectors.
- Return all trapezoid coordinates instead of just the count, using the same slope hash approach.
- Limit to trapezoids with horizontal bases to simplify slope handling.
How GhostInterview Helps
- Automatically normalizes slopes and hashes point pairs to find trapezoids efficiently.
- Highlights disjoint pair combinations to avoid overcounting while scanning arrays.
- Provides step-by-step trapezoid counting tailored to Array scanning plus hash lookup patterns.
Topic Pages
Related GhostInterview Pages
- LeetCode Interview Copilot - Use GhostInterview as a live solver when you want direct help with LeetCode-style coding questions.
- Coding Interview Assistant - See how GhostInterview supports array, string, linked list, graph, and tree interview workflows.
- How GhostInterview Works - Review the screenshot, reasoning, and answer flow before using the solver in a live interview.
FAQ
What is the key pattern to solve Count Number of Trapezoids II efficiently?
The primary pattern is scanning all point pairs and using a hash map keyed by normalized slope to detect parallel sides quickly.
How do I handle slope precision in this problem?
Compute slope as a fraction reduced by GCD and fix signs consistently to avoid floating-point errors.
Can I use a brute-force approach?
Brute-force four-point iteration is too slow for n up to 500; using hash-based slope groups is required for efficiency.
Are overlapping point pairs allowed when counting trapezoids?
No, each trapezoid must use four distinct points; overlapping pairs lead to invalid counts.
How does array scanning plus hash lookup help here?
It allows grouping point pairs by slope and quickly finding combinations that form trapezoids without iterating all quadruples.
Need direct help with Count Number of Trapezoids II 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 II from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.
Capture the prompt fast instead of rewriting the problem by hand.
Get the solution path, trade-offs, and complexity summary in one pass.
Stay outside captured layers on supported screen-share workflows.
Stay in the same pattern family
Given a list of distinct points, count the number of unique horizontal trapezoids that can be formed by selecting four points.
Open problem page#3588 Find Maximum Area of a TriangleFind the maximum area of a triangle from 2D coordinates with at least one side parallel to the x-axis or y-axis.
Open problem page#3591 Check if Any Element Has Prime FrequencyCheck if any element in the array has a prime frequency count, leveraging array scanning and hash table lookup.
Open problem page