Skip to content
← All posts

Widest Vertical Area Between Two Points Containing No Points

5 min read
leetcodeproblemeasyarrayssorting

LeetCode 1637, Widest Vertical Area Between Two Points Containing No Points, asks you to find the widest vertical strip on a 2D plane that contains none of the given points on its interior. Despite the long name, the solution is surprisingly short once you see what the problem is really asking.

The problem

You are given n points on a 2D plane where points[i] = [xi, yi]. A vertical area is a fixed-width region that extends infinitely along the y-axis. Return the widest vertical area between any two points such that no point lies strictly inside the area. Note that points on the edge of the area are allowed.

The key realization is that y-coordinates are completely irrelevant. A vertical area stretches infinitely in the y-direction, so the only thing that determines its width is the x-coordinates of the points that form its left and right boundaries.

02468100246810gap = 2(8,7)(9,9)(7,4)(9,7)(1,1)(3,5)(5,2)
Seven points plotted on a coordinate plane. The shaded region between x=1 and x=3 shows the widest vertical area (width 2) that contains no other points.

The brute force approach

The naive approach is to check every pair of points, compute the horizontal distance between them, and then verify that no other point has an x-coordinate strictly between the two. For n points, there are O(n^2) pairs, and checking each pair against all other points takes O(n) time, giving O(n^3) overall. This works but is far slower than necessary.

You could improve the brute force slightly by only looking at x-coordinates and checking pairs, but you would still be doing O(n^2) pair comparisons. The real trick is recognizing that you do not need to check arbitrary pairs at all.

The key insight

The widest vertical area that contains no points must be bounded by two points whose x-coordinates are consecutive in sorted order. If there were any x-coordinate between them, the area would be narrower or it would contain a point.

This means the problem reduces to: sort the x-coordinates, then find the maximum difference between any two consecutive values in the sorted list. That is the entire algorithm.

Whenever a problem asks about "gaps" or "regions between elements," sorting and scanning consecutive differences is usually the right first move. This pattern shows up in Maximum Gap, bucket sort problems, and interval scheduling.

Step-by-step walkthrough

Step 1: Extract all x-coordinates from the input points.

points(8,7)(9,9)(7,4)(9,7)(1,1)(3,5)(5,2)x-coords8979135

We only care about x-coordinates. The y-values do not affect vertical area widths.

Step 2: Sort the x-coordinates in ascending order.

unsorted8979135sorted1357899

Sorting brings adjacent x-values next to each other, so consecutive differences give all possible vertical gaps.

Step 3: Compute gaps between consecutive sorted x-values.

sorted1357899gaps222110

Gaps: 3-1=2, 5-3=2, 7-5=2, 8-7=1, 9-8=1, 9-9=0.

Step 4: Return the maximum gap. Answer = 2.

gaps222110result2

The three gaps of size 2 are all tied for the maximum. The widest vertical area has width 2.

The code

def maxWidthOfVerticalArea(points: list[list[int]]) -> int:
    xs = sorted(p[0] for p in points)
    return max(xs[i + 1] - xs[i] for i in range(len(xs) - 1))

The solution is three lines. First, we extract all x-coordinates and sort them. The generator expression p[0] for p in points pulls out just the first element of each point. Sorting takes O(n log n) time.

Then we compute the maximum difference between consecutive elements in the sorted list. The expression xs[i + 1] - xs[i] gives the gap between each pair of neighbors. We take the maximum of all these gaps, which is the widest vertical area.

Notice that we do not need to deduplicate the x-coordinates. If two points share the same x-value, the gap between them is 0, which will never be the maximum (unless all points share the same x-coordinate, in which case 0 is the correct answer). Sorting handles duplicates naturally.

The y-coordinates are never used. This is a common trap in geometry problems: the problem statement includes information that is completely irrelevant to the solution. Recognizing what to ignore is just as important as knowing what to compute.

Complexity analysis

ApproachTimeSpace
Brute force (all pairs + validation)O(n^3)O(1)
Optimal (sort + scan)O(n log n)O(n)

Time is O(n log n) because sorting dominates. The single pass to find the maximum gap is O(n), which is absorbed by the sort. You cannot do better than O(n log n) with a comparison-based approach, since you need to establish the ordering of x-coordinates.

Space is O(n) because we create a sorted list of all x-coordinates. If you sorted the original points array in-place by x-coordinate, you could reduce space to O(1), but the time complexity would remain the same.

The building blocks

Sort-then-scan

Many problems follow this template: sort the data, then make a single pass to compute the answer. You see it in problems like Maximum Gap, Merge Intervals, and 3Sum. Sorting eliminates the need to check every pair by ensuring that the relevant neighbors are adjacent. Whenever a problem involves "closest," "widest," or "maximum gap" between elements, try sorting first and scanning consecutive pairs.

Dimension reduction

This problem hands you 2D points but the answer depends on only one dimension. Stripping away the irrelevant dimension simplifies the problem dramatically, from a 2D geometry question to a 1D array question. Watch for this in problems where one axis is unbounded or irrelevant. Reducing the dimensionality before solving is a powerful technique.

Edge cases

Two points. With exactly two points, there is only one gap, which is |x1 - x2|. The sort-and-scan approach handles this correctly.

All points share the same x-coordinate. Every consecutive gap is 0, so the answer is 0. No special handling is needed.

Duplicate x-coordinates mixed with distinct ones. Duplicates produce gaps of 0 in the sorted list. The maximum is still computed correctly because max simply ignores the zeros in favor of larger gaps.

From understanding to recall

The hard part of this problem is not the code. It is seeing that y-coordinates do not matter and that the answer is just the maximum consecutive gap in sorted x-values. Once you have that insight, the implementation is two or three lines.

But insights like this need to be practiced until they become reflexes. The next time you see a problem about "regions," "gaps," or "areas between points," you want your brain to immediately suggest sorting and scanning. Spaced repetition drills this pattern into long-term memory. You solve it once, revisit it a few days later, then a week later, and the sort-then-scan reflex becomes automatic.

Related posts

  • Sort Colors - another problem where sorting is the key step
  • Maximum Gap - finding the max gap between consecutive elements in sorted order
  • Merge Intervals - sorting-based approach for interval problems