Count Number of Trapezoids I solution and explanation
9 more videos on this problem
Count Number of Trapezoids I | Simplest Explanation | Detailed | Leetcode 3623 | codestorywithMIKcodestorywithMIK · 22:03 · 7K views
Count Number of Trapezoids I | LeetCode 3623 | Complete Intuition ExplainedSanyam IIT Guwahati · 15:16 · 2K views
3623 Count Number of Trapezoids I LeetCodeR Sai Siddhu · 17:25 · 1K views
3623. Count Number of Trapezoids I | Leetcode Daily - PythonLeetcode Daily · 10:02 · 1K views
3623. Count Number of Trapezoids I (Leetcode Medium)Programming Live with Larry · 15:51 · 1K views
Daily Leetcode #2072: Dec 02, 2025 - 3623. Count Number of Trapezoids IProgramming Live with Larry · 14:10 · 764 views
Count Number of Trapezoids I | LeetCode 3623 - Pythonimpoldev · 12:55 · 284 views
question no.-3623 || Count Number of Trapezoids I || Leetcode Daily ProblemLeetCodewithManish · 7:22 · 197 views
3623. Count Number of Trapezoids I | Detailed Video | Leetcode Daily ChallengeEye on AI and DSA · 36:49 · 161 views
Count Number of Trapezoids I | LeetCode 3623 | Java Code | Developer CoderDeveloper Coder · 27:59 · 134 views
The problem
You are given a 2D integer array points, where points[i] = [xi, yi] represents the coordinates of the ith point on the Cartesian plane.
A horizontal trapezoid is a convex quadrilateral with at least one pair of horizontal sides (i.e. parallel to the x-axis). Two lines are parallel if and only if they have the same slope.
Return the number of unique horizontal trapezoids that can be formed by choosing any four distinct points from points.
Since the answer may be very large, return it modulo 109 + 7.
Example 1:
Input: points = [[1,0],[2,0],[3,0],[2,2],[3,2]]
Output: 3
Explanation:

There are three distinct ways to pick four points that form a horizontal trapezoid:
- Using points
[1,0],[2,0],[3,2], and[2,2]. - Using points
[2,0],[3,0],[3,2], and[2,2]. - Using points
[1,0],[3,0],[3,2], and[2,2].
Example 2:
Input: points = [[0,0],[1,0],[0,1],[2,1]]
Output: 1
Explanation:

There is only one horizontal trapezoid that can be formed.
Constraints:
4 <= points.length <= 105–108 <= xi, yi <= 108- All points are pairwise distinct.
Approach overview
Problem Overview: You are given points on a 2D plane. A trapezoid requires at least one pair of parallel sides. The key observation is that any pair of points sharing the same y-coordinate forms a horizontal segment, and two horizontal segments on different y levels can act as the parallel sides of a trapezoid.
Approach 1: Enumeration with Y-Level Grouping (O(n + k2) time, O(n) space)
Group points by their y-coordinate using a hash map. If a level has m points, you can form C(m,2) horizontal segments on that line. Each segment represents a possible base of a trapezoid. Store the number of segments for every y-level.
Next, enumerate every pair of distinct y-levels. If level i has a horizontal segments and level j has b, then you can form a * b trapezoids using one segment from each level as the parallel sides. The non-parallel sides are implicitly defined by connecting the endpoints. Summing this product across all pairs of levels gives the total trapezoid count.
The hash table groups points efficiently, while simple array enumeration handles combinations of levels. The geometric insight is that parallel horizontal segments guarantee a trapezoid regardless of the remaining edges. This reduces a geometric counting problem to a combination counting problem using math and hashing.
Space complexity is O(n) to store the frequency of points per y-level. Time complexity is dominated by counting segments and iterating over pairs of distinct levels. If there are k unique y-coordinates, the pair enumeration costs O(k^2).
Recommended for interviews: The hash map + enumeration approach is the expected solution. It shows you can convert a geometry problem into counting combinations. A brute force check of all quadruples of points would be O(n^4) and only demonstrates baseline understanding, while grouping by y-coordinate and counting segment pairs shows stronger algorithmic reasoning.
Solution
According to the problem description, horizontal edges have the same y coordinate. Therefore, we can group points by their y coordinates and count the number of points for each y coordinate.
We use a hash table cnt to store the number of points for each y coordinate. For each y coordinate y_i, assuming the number of corresponding points is v, the number of ways to select two points from these points as a horizontal edge is \binom{v}{2} = \frac{v(v-1)}{2}, denoted as t.
We use a variable s to record the sum of the number of horizontal edges for all previous y coordinates. Then, we can multiply the number of horizontal edges t for the current y coordinate by the sum s of the number of horizontal edges for all previous y coordinates to get the number of trapezoids with the current y coordinate as one pair of horizontal edges, and add it to the answer. Finally, we add the number of horizontal edges t for the current y coordinate to s for subsequent calculations.
Note that since the answer may be very large, we need to take the modulo 10^9 + 7.
The time complexity is O(n) and the space complexity is O(n), where n is the number of points.
class Solution:
def countTrapezoids(self, points: List[List[int]]) -> int:
mod = 10**9 + 7
cnt = Counter(p[1] for p in points)
ans = s = 0
for v in cnt.values():
t = v * (v - 1) // 2
ans = (ans + s * t) % mod
s += t
return ansclass Solution {
public int countTrapezoids(int[][] points) {
final int mod = (int) 1e9 + 7;
Map<Integer, Integer> cnt = new HashMap<>();
for (var p : points) {
cnt.merge(p[1], 1, Integer::sum);
}
long ans = 0, s = 0;
for (int v : cnt.values()) {
long t = 1L * v * (v - 1) / 2;
ans = (ans + s * t) % mod;
s += t;
}
return (int) ans;
}
}class Solution {
public:
int countTrapezoids(vector<vector<int>>& points) {
const int mod = 1e9 + 7;
unordered_map<int, int> cnt;
for (auto& p : points) {
cnt[p[1]]++;
}
long long ans = 0, s = 0;
for (auto& [_, v] : cnt) {
long long t = 1LL * v * (v - 1) / 2;
ans = (ans + s * t) % mod;
s += t;
}
return (int) ans;
}
};function countTrapezoids(points: number[][]): number {
const mod = 1_000_000_007;
const cnt = new Map<number, number>();
for (const p of points) {
cnt.set(p[1], (cnt.get(p[1]) ?? 0) + 1);
}
let ans = 0;
let s = 0;
for (const v of cnt.values()) {
const t = (v * (v - 1)) / 2;
const mul = BigInt(s) * BigInt(t);
ans = Number((BigInt(ans) + mul) % BigInt(mod));
s += t;
}
return ans;
}func countTrapezoids(points [][]int) int {
const mod = 1_000_000_007
cnt := make(map[int]int)
for _, p := range points {
cnt[p[1]]++
}
var ans, s int64
for _, v := range cnt {
t := int64(v) * int64(v-1) / 2
ans = (ans + s*t) % mod
s += t
}
return int(ans)
}Complexity analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Quadruple Enumeration | O(n^4) | O(1) | Conceptual baseline; useful for verifying small inputs |
| Enumerate All Segments and Check Parallelism | O(n^2) | O(n^2) | General geometry problems where slopes must be compared |
| Hash Map by Y-Level + Segment Pair Enumeration | O(n + k^2) | O(n) | Optimal for horizontal parallel sides; typical interview solution |
Frequently asked questions
Is Count Number of Trapezoids I easy or hard?
Count Number of Trapezoids I is typically classified as Medium difficulty. The challenge is recognizing that trapezoids can be counted by grouping horizontal segments and applying combinatorics rather than explicitly constructing quadrilaterals.
Count Number of Trapezoids I Python/Java solution
Most implementations first build a hash map from y-coordinate to point count. Then compute combinations C(m,2) for each level and iterate through pairs of levels multiplying their segment counts. The same logic translates directly to Python, Java, C++, Go, and TypeScript.
How to solve Count Number of Trapezoids I in O(n)?
Strict O(n) is generally not achievable because you must consider combinations of distinct y-levels. The closest practical solution is O(n + k^2) by grouping points by y-coordinate and counting horizontal segments. Pairing segment counts across levels efficiently counts all trapezoids.
What is the best approach for Count Number of Trapezoids I?
The most efficient approach groups points by their y-coordinate using a hash map. For each y-level with m points, compute C(m,2) horizontal segments. Then enumerate pairs of y-levels and multiply their segment counts to get the number of trapezoids. This reduces the geometric problem to combination counting with O(n + k^2) time.
Is Count Number of Trapezoids I asked at Google/Amazon/Meta?
Geometry counting problems involving hash maps and combinations appear in interviews at companies like Google, Amazon, and Meta. Variations often test whether candidates can transform geometric constraints into frequency counting or combinatorial formulas.
What data structure is used in Count Number of Trapezoids I?
A hash table (hash map) is the primary data structure. It stores the number of points for each y-coordinate, allowing quick calculation of horizontal segment counts and efficient enumeration of level pairs.
What is the time complexity of Count Number of Trapezoids I?
The optimized solution runs in O(n + k^2) time where n is the number of points and k is the number of unique y-coordinates. Building the hash map takes O(n), computing segment counts is linear, and enumerating pairs of y-levels costs O(k^2). Space complexity is O(n).
