Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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:
[1,0], [2,0], [3,2], and [2,2].[2,0], [3,0], [3,2], and [2,2].[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 <= 108Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Math
[[1,0],[2,0],[3,0],[2,2],[3,2]]
[[0,0],[1,0],[0,1],[2,1]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3623: Count Number of Trapezoids I
class 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;
}
}
// Accepted solution for LeetCode #3623: Count Number of Trapezoids I
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)
}
# Accepted solution for LeetCode #3623: Count Number of Trapezoids I
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 ans
// Accepted solution for LeetCode #3623: Count Number of Trapezoids I
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3623: Count Number of Trapezoids I
// class 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;
// }
// }
// Accepted solution for LeetCode #3623: Count Number of Trapezoids I
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;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.