Maximum Gap

Given an unsorted array, return the maximum difference between successive elements in its sorted form. Bucket-based counting is the standard way to solve it in linear time when the value range is large.

Input Format

nums = array of non-negative integers

Output Format

maximum successive gap in sorted order

Constraints

  • 1 <= nums.length <= 10^5; 0 <= nums[i] <= 10^9

Examples

Example 1:

Input:

nums = [3,6,9,1]

Output:

3

Explanation:

The sorted array is [1,3,6,9], and the largest gap is 3.

Example 2:

Input:

nums = [10]

Output:

0

Explanation:

With fewer than two numbers, the gap is 0.

Loading...
Maximum Gap - Sorting Based Array Problems