Program to find out the vertical area between two points where no point lies and is the widest in Python

Suppose, we are given n number of points as (x, y). A vertical area is an area that is extended infinitely along the y-axis. We have to find out the vertical area between two points such that no other point is inside the area and is the widest.

So, if the input is like pts = [[10,9],[11,11],[9,6],[11,9]], then the output will be 1.

(9,6) (10,9) (11,11) (11,9) x y

The areas in red and blue are optimal vertical areas with no points inside them. The widest area has width 1.

Algorithm Steps

To solve this problem, we will follow these steps −

  • Sort the list of points by x-coordinate

  • For each adjacent pair of points, calculate the horizontal distance

  • Return the maximum distance found

Example

Let us see the following implementation to get better understanding ?

def solve(pts):
    pts.sort()
    return max(pts[i][0] - pts[i - 1][0] for i in range(1, len(pts)))

# Test with the given example
points = [[10,9],[11,11],[9,6],[11,9]]
result = solve(points)
print(f"Widest vertical area: {result}")
Widest vertical area: 1

How It Works

The algorithm works by sorting points by their x-coordinates: [9,6], [10,9], [11,9], [11,11]. Then it calculates gaps between consecutive x-coordinates:

  • Gap between x=9 and x=10: 10 - 9 = 1

  • Gap between x=10 and x=11: 11 - 10 = 1

  • Gap between x=11 and x=11: 11 - 11 = 0

The maximum gap is 1, which represents the widest vertical area with no points inside.

Complete Example with Explanation

def solve(pts):
    # Sort points by x-coordinate
    pts.sort()
    print(f"Sorted points: {pts}")
    
    # Find maximum gap between consecutive x-coordinates
    max_gap = 0
    for i in range(1, len(pts)):
        gap = pts[i][0] - pts[i - 1][0]
        print(f"Gap between x={pts[i-1][0]} and x={pts[i][0]}: {gap}")
        max_gap = max(max_gap, gap)
    
    return max_gap

# Test cases
test_cases = [
    [[10,9],[11,11],[9,6],[11,9]],
    [[1,5],[3,2],[8,1],[5,4]],
    [[0,0],[1,1],[2,2]]
]

for i, points in enumerate(test_cases, 1):
    print(f"\nTest case {i}: {points}")
    result = solve(points.copy())  # Use copy to avoid modifying original
    print(f"Widest vertical area: {result}")
Test case 1: [[10, 9], [11, 11], [9, 6], [11, 9]]
Sorted points: [[9, 6], [10, 9], [11, 9], [11, 11]]
Gap between x=9 and x=10: 1
Gap between x=10 and x=11: 1
Gap between x=11 and x=11: 0
Widest vertical area: 1

Test case 2: [[1, 5], [3, 2], [8, 1], [5, 4]]
Sorted points: [[1, 5], [3, 2], [5, 4], [8, 1]]
Gap between x=1 and x=3: 2
Gap between x=3 and x=5: 2
Gap between x=5 and x=8: 3
Widest vertical area: 3

Test case 3: [[0, 0], [1, 1], [2, 2]]
Sorted points: [[0, 0], [1, 1], [2, 2]]
Gap between x=0 and x=1: 1
Gap between x=1 and x=2: 1
Widest vertical area: 1

Conclusion

The solution efficiently finds the widest vertical area by sorting points and calculating maximum gaps between x-coordinates. The time complexity is O(n log n) due to sorting, and space complexity is O(1).

Updated on: 2026-03-26T13:59:32+05:30

230 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements