Maximum Gap

Given an integer array nums, return the maximum difference between two successive elements in its sorted form. If the array contains fewer than two elements, return 0. You must write an algorithm that runs in linear time and uses linear extra space.

30:00

Maximum Gap
medium
Free written solution
Topics
Companies

Given an integer array nums, return the maximum difference between two successive elements in its sorted form. If the array contains fewer than two elements, return 0. You must write an algorithm that runs in linear time and uses linear extra space.

Example 1:
Input: {"nums":[3,6,9,1]}
Output: 3
Constraints:
  • 1≤nums.length≤1051 \leq \text{nums.length} \leq 10^5

  • 0≤nums[i]≤1090 \leq \text{nums}[i] \leq 10^9

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[3,6,9,1]}

We want the widest jump between neighbours once nums is sorted, without actually sorting. There are 4 numbers.

nums (unsorted, read once)

3
6
9
1
prev = -
best = 0
Variables
VariableValue
n4
lo-
hi-
size-
buckets-
prev-
best0
DepthFunction Call
Stack empty
0/14