Widest Vertical Area Between Two Points Containing No Points - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: points = [[8,7],[9,9],[7,4],[9,7]]
Output: 1
💡 Note: X-coordinates are [8,9,7,9]. After sorting: [7,8,9,9]. Gaps between consecutive unique x-values: 8-7=1, 9-8=1. Maximum gap is 1.
Example 2 — Larger Gaps
$ Input: points = [[3,1],[9,0],[1,0],[1,4],[5,3],[8,8]]
Output: 3
💡 Note: X-coordinates sorted: [1,3,5,8,9]. Gaps: 3-1=2, 5-3=2, 8-5=3, 9-8=1. Maximum gap is 3.
Example 3 — Minimum Case
$ Input: points = [[1,0],[2,0]]
Output: 1
💡 Note: Only two points with x-coordinates [1,2]. The gap between them is 2-1=1.

Constraints

  • n == points.length
  • 2 ≤ n ≤ 105
  • points[i].length == 2
  • -108 ≤ xi, yi ≤ 108

Visualization

Tap to expand
Widest Vertical Area: Find Maximum X-Coordinate GapInput Points:(7,4)(8,7)(9,9)(9,7)Sort X-coordsSorted X-coords:789Gap: 1Gap: 1Vertical AreaWidth = 1Height = ∞Maximum Vertical Area Width: 1
Understanding the Visualization
1
Input Points
Points scattered on 2D plane: [[8,7],[9,9],[7,4],[9,7]]
2
Extract X-Coordinates
Only x-values matter: [8,9,7,9] → sorted: [7,8,9,9]
3
Find Maximum Gap
Consecutive gaps: 8-7=1, 9-8=1 → Maximum width = 1
Key Takeaway
🎯 Key Insight: Y-coordinates are irrelevant - only sort x-coordinates and find maximum consecutive gap
Asked in
Amazon 15 Google 12
25.0K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen