Minimize the Maximum Adjacent Element Difference

You are given an array of integers nums. Some values in nums are missing and are denoted by -1.

You must choose a pair of positive integers (x, y) exactly once and replace each missing element with either x or y.

You need to minimize the maximum absolute difference between adjacent elements of nums after replacements.

Return the minimum possible difference.

Example 1
Inputnums = [1,2,-1,10,8]
Output4
Choosing (6, 7) allows nums to become [1, 2, 6, 10, 8], whose maximum adjacent absolute difference is 4.
Example 2
Inputnums = [-1,-1,-1]
Output0
Choosing (4, 4) allows nums to become [4, 4, 4], so every adjacent absolute difference is 0.

Constraints

  • 2 <= nums.length <= 10^5
  • nums[i] is either -1 or in the range [1, 10^9].

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate