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.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
Given n points on a 2D plane where points[i] = [xi, yi], Return the widest vertical area between two points such that no points are inside the area.
A vertical area is an area of fixed-width extending infinitely along the y-axis (i.e., infinite height). The widest vertical area is the one with the maximum width.
Note that points on the edge of a vertical area are not considered included in the area.
Example 1:
Input: points = [[8,7],[9,9],[7,4],[9,7]] Output: 1 Explanation: Both the red and the blue area are optimal.
Example 2:
Input: points = [[3,1],[9,0],[1,0],[1,4],[5,3],[8,8]] Output: 3
Constraints:
n == points.length2 <= n <= 105points[i].length == 20 <= xi, yi <= 109Problem summary: Given n points on a 2D plane where points[i] = [xi, yi], Return the widest vertical area between two points such that no points are inside the area. A vertical area is an area of fixed-width extending infinitely along the y-axis (i.e., infinite height). The widest vertical area is the one with the maximum width. Note that points on the edge of a vertical area are not considered included in the area.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[[8,7],[9,9],[7,4],[9,7]]
[[3,1],[9,0],[1,0],[1,4],[5,3],[8,8]]
maximum-gap)maximum-consecutive-floors-without-special-floors)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1637: Widest Vertical Area Between Two Points Containing No Points
class Solution {
public int maxWidthOfVerticalArea(int[][] points) {
Arrays.sort(points, (a, b) -> a[0] - b[0]);
int ans = 0;
for (int i = 0; i < points.length - 1; ++i) {
ans = Math.max(ans, points[i + 1][0] - points[i][0]);
}
return ans;
}
}
// Accepted solution for LeetCode #1637: Widest Vertical Area Between Two Points Containing No Points
func maxWidthOfVerticalArea(points [][]int) (ans int) {
sort.Slice(points, func(i, j int) bool { return points[i][0] < points[j][0] })
for i, p := range points[1:] {
ans = max(ans, p[0]-points[i][0])
}
return
}
# Accepted solution for LeetCode #1637: Widest Vertical Area Between Two Points Containing No Points
class Solution:
def maxWidthOfVerticalArea(self, points: List[List[int]]) -> int:
points.sort()
return max(b[0] - a[0] for a, b in pairwise(points))
// Accepted solution for LeetCode #1637: Widest Vertical Area Between Two Points Containing No Points
struct Solution;
use std::collections::HashSet;
impl Solution {
fn max_width_of_vertical_area(points: Vec<Vec<i32>>) -> i32 {
let mut x_set: HashSet<i32> = HashSet::new();
for point in points {
x_set.insert(point[0]);
}
let mut x_arr: Vec<i32> = x_set.into_iter().collect();
x_arr.sort_unstable();
let mut res = 0;
for w in x_arr.windows(2) {
res = res.max(w[1] - w[0]);
}
res
}
}
#[test]
fn test() {
let points = vec_vec_i32![[8, 7], [9, 9], [7, 4], [9, 7]];
let res = 1;
assert_eq!(Solution::max_width_of_vertical_area(points), res);
let points = vec_vec_i32![[3, 1], [9, 0], [1, 0], [1, 4], [5, 3], [8, 8]];
let res = 3;
assert_eq!(Solution::max_width_of_vertical_area(points), res);
}
// Accepted solution for LeetCode #1637: Widest Vertical Area Between Two Points Containing No Points
function maxWidthOfVerticalArea(points: number[][]): number {
points.sort((a, b) => a[0] - b[0]);
let ans = 0;
for (let i = 1; i < points.length; ++i) {
ans = Math.max(ans, points[i][0] - points[i - 1][0]);
}
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.