Problem 362062 · medium · Phase 03 Linear Management & Searching

Smallest Span Touching Both Lists

two pointers · sorted arrays

Given two non-empty lists of integers a and b, both sorted in non-decreasing order, pick one value from each so that the closed span between them is as short as possible. Return that span as [lo, hi] (lo <= hi). If several spans have the same minimal length, return the one with the smallest lo.

Examples

Input:  a = [1, 4, 10], b = [6, 7, 12]
Output: [4, 6]
Explanation: [4, 6] and [10, 12] both have length 2; the smaller lo wins.

Input:  a = [5], b = [1, 9]
Output: [1, 5]

Constraints

  • 1 <= len(a), len(b) <= 10**5
  • Target: O(len(a) + len(b)) time, O(1) extra space.

Goals

  • Walk two sorted lists like a merge and examine each cross-list neighbouring pair
  • Apply a tie-break consistently while scanning
Starting Python…