Maximum Gap

Given an integer array nums, return the maximum difference between two successive elements in its sorted form.

If nums contains fewer than two elements, return 0.

Your solution must run in O(n) time and use O(n) extra space.

Example 1
Inputnums = [3,6,9,1]
Output3
After sorting, the array is [1, 3, 6, 9], and the maximum successive difference is 3.
Example 2
Inputnums = [10]
Output0
There is only one element, so there are no successive elements to compare.

Constraints

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

Asked at 7 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