Maximum Gap

Medium Sorting AI round

nums is a list of whole numbers, none of them negative.

Put the numbers of nums in order from smallest to largest. A gap is the difference between two numbers next to each other in that order.

Return the widest gap, or 0 if nums has only one number.

Example 1

Input: nums = [14,2,30,9,21]
Output: 9

In order the numbers are 2, 9, 14, 21 and 30, and the gaps between neighbours are 7, 5, 7 and 9. The widest is 9, from 21 to 30.

Example 2

Input: nums = [100,100,7]
Output: 93

Example 3

Input: nums = [7,7,7]
Output: 0

Constraints

  • 1 ≤ nums.length ≤ 2 × 10⁵
  • 0 ≤ nums[i] ≤ 10⁹

More Sorting

All Sorting problems