Talentd
FleetCode
Make and follow sheets.Sign in

Widest Vertical Area Between Two Points Containing No Points solution and explanation

EasyArraySorting4 min read87.3% acceptanceUpdated 9 Apr 2026
Wildest Vertical Area Between Two Points Containing No Points - Leetcode 1637 - PythonNeetCodeIO · 5:21 · 6K viewsWatch on YouTube
9 more videos on this problem
  1. Wildest Vertical Area Between Two Points Containing No Points - Leetcode 1637 - PythonNeetCodeIO · 5:21 · 6K views
  2. Widest Vertical Area Between Two Points Containing No Points | Leetcode 1637codestorywithMIK · 9:02 · 4K views
  3. 1637. Widest Vertical Area Between Two Points Containing No PointsAryan Mittal · 4:02 · 1K views
  4. LeetCode 1637 : C++ Solution | Widest Vertical Area Between Two Points Containing No PointsAmit Manjarly · 6:00 · 989 views
  5. LeetCode 1637 : JAVA Solution | Widest Vertical Area Between Two Points Containing No PointsAmit Manjarly · 6:37 · 925 views
  6. Leetcode | 1637. Widest Vertical Area Between Two Points Containing No Points | Medium Java SolutionDeveloper Docs · 5:46 · 765 views
  7. Solving LeetCode 1637 Widest Vertical Area Between Two Points Containing No Points | MicrosoftCode Harmony · 6:53 · 599 views
  8. 1637. Widest Vertical Area Between Two Points Containing No Points (Leetcode Medium)Programming Live with Larry · 4:38 · 457 views
  9. Leetcode 1637 Widest Vertical Area Between Two Points Containing No PointsFraz · 3:17 · 435 views
  10. 1637. Widest Vertical Area Between Two Points Containing No Points - JavaScript - LeetCodeLeetCodeWithMonu · 8:18 · 201 views

The 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.

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.length
  • 2 <= n <= 105
  • points[i].length == 2
  • 0 <= xi, yi <= 109

Approach overview

Problem Overview: You receive a list of 2D points. A vertical area is the space between two vertical lines (x = a and x = b) that contains no points between them. The task is to return the maximum width of such an area. Only the x-coordinates matter because vertical gaps depend purely on horizontal distance.

Approach 1: Sort and Compare Consecutive Differences (O(n log n) time, O(1) extra space or O(n) depending on sorting)

The key observation: the widest empty vertical region must lie between two neighboring x-coordinates after sorting. Extract all x values from the points and sort them. Then iterate through the sorted list and compute the difference between each consecutive pair. Track the maximum difference seen. Sorting ensures that any larger gap would appear between adjacent elements in the ordered sequence. This approach is simple, reliable, and commonly expected in interviews. It relies on concepts from Array traversal and Sorting.

Approach 2: Bucket Sort for Near-Constant Time Gap Calculation (O(n) time, O(n) space)

Sorting works well, but the problem can be solved in linear time using a bucket-based gap strategy similar to the maximum gap problem. First determine the global minimum and maximum x-values. Divide the range into buckets sized so that the expected gap between buckets is roughly (maxX - minX) / (n - 1). Each bucket stores only the minimum and maximum x-values that fall inside it. After distributing points into buckets, iterate through them and compute the gap between the maximum of the previous non-empty bucket and the minimum of the current one. The largest such gap is the answer. Because points inside the same bucket cannot produce the maximum gap, comparisons only occur between buckets. This method avoids full sorting while still using ideas from Array processing and distribution-based Sorting techniques.

Recommended for interviews: The sorting approach is what most interviewers expect. It is easy to implement, easy to reason about, and runs in O(n log n), which is more than sufficient for typical constraints. The bucket-based solution demonstrates deeper algorithmic knowledge and achieves O(n) time, but it is usually presented as an optimization rather than the first solution. Showing the sorting solution first proves you understand the core observation that the widest vertical area must exist between consecutive sorted x-values.

Approach 1: Approach 1: Sort and Compare Consecutive Differences

This approach involves sorting the x-coordinates of the points and then finding the maximum difference between consecutive sorted x-coordinates. This difference gives the widest vertical area between any two points.

def maxWidthOfVerticalArea(points):
    x_coords = [point[0] for point in points]
    x_coords.sort()
    max_width = 0
    for i in range(1, len(x_coords)):
        max_width = max(max_width, x_coords[i] - x_coords[i - 1])
    return max_width

if __name__ == "__main__":
    points = [[3, 1], [9, 0], [1, 0], [1, 4], [5, 3], [8, 8]]
    print(maxWidthOfVerticalArea(points))
import java.util.Arrays;

public class Solution {
    public int maxWidthOfVerticalArea(int[][] points) {
        int[] xCoords = new int[points.length];
        for (int i = 0; i < points.length; i++) {
            xCoords[i] = points[i][0];
        }
        Arrays.sort(xCoords);
        int maxWidth = 0;
        for (int i = 1; i < xCoords.length; i++) {
            maxWidth = Math.max(maxWidth, xCoords[i] - xCoords[i - 1]);
        }
        return maxWidth;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[][] points = {{3, 1}, {9, 0}, {1, 0}, {1, 4}, {5, 3}, {8, 8}};
        System.out.println(solution.maxWidthOfVerticalArea(points));
    }
}
#include <iostream>
#include <vector>
#include <algorithm>

int maxWidthOfVerticalArea(std::vector<std::vector<int>>& points) {
    std::vector<int> xCoords;
    for (const auto& point : points) {
        xCoords.push_back(point[0]);
    }
    std::sort(xCoords.begin(), xCoords.end());
    int maxWidth = 0;
    for (size_t i = 1; i < xCoords.size(); ++i) {
        maxWidth = std::max(maxWidth, xCoords[i] - xCoords[i - 1]);
    }
    return maxWidth;
}

int main() {
    std::vector<std::vector<int>> points = {{3, 1}, {9, 0}, {1, 0}, {1, 4}, {5, 3}, {8, 8}};
    std::cout << maxWidthOfVerticalArea(points) << std::endl;
    return 0;
}
function maxWidthOfVerticalArea(points) {
    let xCoords = points.map(point => point[0]);
    xCoords.sort((a, b) => a - b);
    let maxWidth = 0;
    for (let i = 1; i < xCoords.length; i++) {
        maxWidth = Math.max(maxWidth, xCoords[i] - xCoords[i - 1]);
    }
    return maxWidth;
}

console.log(maxWidthOfVerticalArea([[3, 1], [9, 0], [1, 0], [1, 4], [5, 3], [8, 8]]));
#include <stdio.h>
#include <stdlib.h>

int compare(const void* a, const void* b) {
    return (*(int*)a - *(int*)b);
}

int maxWidthOfVerticalArea(int points[][2], int pointsSize) {
    int xCoords[pointsSize];
    for (int i = 0; i < pointsSize; i++) {
        xCoords[i] = points[i][0];
    }
    qsort(xCoords, pointsSize, sizeof(int), compare);
    int maxWidth = 0;
    for (int i = 1; i < pointsSize; i++) {
        int width = xCoords[i] - xCoords[i - 1];
        if (width > maxWidth) {
            maxWidth = width;
        }
    }
    return maxWidth;
}

int main() {
    int points[][2] = {{3, 1}, {9, 0}, {1, 0}, {1, 4}, {5, 3}, {8, 8}};
    int pointsSize = sizeof(points) / sizeof(points[0]);
    printf("%d\n", maxWidthOfVerticalArea(points, pointsSize));
    return 0;
}
using System;

class Solution {
    public int maxWidthOfVerticalArea(int[][] points) {
        int[] xCoords = new int[points.Length];
        for (int i = 0; i < points.Length; i++) {
            xCoords[i] = points[i][0];
        }
        Array.Sort(xCoords);
        int maxWidth = 0;
        for (int i = 1; i < xCoords.Length; i++) {
            maxWidth = Math.Max(maxWidth, xCoords[i] - xCoords[i - 1]);
        }
        return maxWidth;
    }

    static void Main() {
        Solution solution = new Solution();
        int[][] points = new int[][] { new int[] { 3, 1 }, new int[] { 9, 0 }, new int[] { 1, 0 }, new int[] { 1, 4 }, new int[] { 5, 3 }, new int[] { 8, 8 } };
        Console.WriteLine(solution.maxWidthOfVerticalArea(points));
    }
}

In Python, list comprehensions efficiently extract x-coordinates, which are then sorted. The maximum gap is calculated using a simple iterative loop.

Complexity

Time Complexity: O(n log n), primarily due to sorting.
Space Complexity: O(n), because we store the x-coordinates separately.

Try this approach in the editor

Approach 2: Approach 2: Bucket Sort for Near-Constant Time Gap Calculation

Although not commonly necessary due to the constraints provided, if x-coordinates were to be over a smaller range, a variant of bucket sort could be used to place x-coordinates in a collection of buckets. This helps find consecutive x-coordinate gaps in nearly constant time, potentially improving performance by eliminating the general-purpose sort with more specialized data placement.

# Bucket approach is explained rather than implemented due to practical concerns with large domain ranges.
// Not implemented due to practical impracticalities with given constraints, useful as educational reflection.
// Similar educational example for C++ as in C.
// Logical exploration of specialized organized sorting for limited numerical spectra.
/* This approach is generally not needed for the given problem constraints but can be experimented with for understanding such tactics */
// Focus is on conceptual understanding instead of actual implementation for this context.

This solution suggests using bucket sorting to illustrate constant time distribution. It remains inefficient for such large numeric spaces but represents keen algorithmic experimentation.

Complexity

Time Complexity: O(n), assuming perfect bucket alignment.
Space Complexity: O(m), with m depending on bucket spread.

Try this approach in the editor

Approach 3: Default Approach

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;
}
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
}
Try this approach in the editor

Complexity comparison

ApproachComplexity
Approach 1: Sort and Compare Consecutive Differences

Time Complexity: O(n log n), primarily due to sorting.
Space Complexity: O(n), because we store the x-coordinates separately.

Approach 2: Bucket Sort for Near-Constant Time Gap Calculation

Time Complexity: O(n), assuming perfect bucket alignment.
Space Complexity: O(m), with m depending on bucket spread.

Default Approach–

Complexity analysis

ApproachTimeSpaceWhen to Use
Sort and Compare Consecutive DifferencesO(n log n)O(1)–O(n)General case. Simplest implementation and commonly expected in coding interviews.
Bucket Sort Gap CalculationO(n)O(n)When linear time is required or when demonstrating advanced understanding of gap-based bucket strategies.

Frequently asked questions

Is Widest Vertical Area Between Two Points Containing No Points easy or hard?

The problem is classified as Easy on LeetCode with a high acceptance rate above 80%. The main insight is realizing that only x-coordinates matter and the answer is the maximum gap between consecutive sorted values. Once that observation is made, the implementation becomes straightforward.

Widest Vertical Area Between Two Points Containing No Points Python/Java solution

The typical Python or Java implementation extracts all x-coordinates, sorts them, and scans for the largest difference between adjacent values. The algorithm runs in O(n log n) time and uses a simple loop after sorting. FleetCode provides implementations in Python, Java, C++, JavaScript, C, and C#.

How to solve Widest Vertical Area Between Two Points Containing No Points in O(n)?

Use a bucket-based strategy similar to the maximum gap algorithm. Compute the minimum and maximum x-values, divide the range into buckets, and store only the min and max x within each bucket. The largest gap will occur between the maximum value of one non-empty bucket and the minimum value of the next non-empty bucket. This avoids full sorting and runs in O(n) time.

What is the best approach for Widest Vertical Area Between Two Points Containing No Points?

The most practical approach sorts all x-coordinates and checks the difference between consecutive values. After sorting, the widest empty vertical region must appear between two adjacent x-values. This solution runs in O(n log n) time and requires minimal additional space, making it the most common interview answer.

Is Widest Vertical Area Between Two Points Containing No Points asked at Google/Amazon/Meta?

This problem follows a classic gap-detection pattern and appears in coding interview practice sets used by companies like Amazon and Google. While the exact question may vary, the underlying idea of sorting coordinates and finding maximum gaps is a common interview concept.

What data structure is used in Widest Vertical Area Between Two Points Containing No Points?

The problem primarily uses arrays to store and process x-coordinates. The typical solution applies sorting to the array of x-values. The linear-time optimization uses buckets (arrays storing min and max values) to compute gaps without sorting the entire dataset.

What is the time complexity of Widest Vertical Area Between Two Points Containing No Points?

The standard solution runs in O(n log n) time because it sorts the x-coordinates of the points. After sorting, a single linear scan computes the maximum gap in O(n). A bucket-based optimization can reduce the overall complexity to O(n) with O(n) extra space.

Comments

Now write it yourselfClose this page and solve Widest Vertical Area Between Two Points Containing No Points in the editor, without looking.
Practise Widest Vertical Area Between Two Points Containing No Points