Ctrl K

Warm Every Cabin

Medium
Cabins stand along a straight road, their coordinates are given in cabins. Stoves are placed along the same road at the coordinates in stoves. All stoves are identical: a stove with reach r standing at point p warms every cabin whose coordinate is between p - r and p + r inclusive. Find the smallest integer reach r such that every cabin is warmed by at least one stove. Several cabins or stoves may share a point, and a cabin may stand at the same point as a stove.
Example 1
cabins=[2, 9, 14], stoves=[6, 12] → 4

The nearest stove to cabin 2 is at 6 (distance 4), to cabin 9 it is 6 or 12 (3), to cabin 14 it is 12 (2). A reach of 4 is needed.

Example 2
cabins=[5, 5, 7], stoves=[5, 7] → 0

Every cabin stands right next to a stove, so a reach of 0 is enough.

Example 3
cabins=[1, 100], stoves=[30] → 70
Constraints
  • 1 ≤ len(cabins), len(stoves) ≤ 3·10⁴
  • 1 ≤ cabins[i], stoves[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTriples Under a Limit MediumPairs With a Fixed Gap MediumStick Triples That Form a Triangle MediumNearest k Values MediumRanked Prime Ratios Medium
loading Python…
Input
cabins=[2, 9, 14], stoves=[6, 12]
Expected
4

Run checks the first 3 tests in your browser. Submit checks all 110 tests on the server, hidden ones included.