hardArrays & HashingPure DSA~30 min
Largest Gap Between Adjacent Sorted Metrics
A telemetry pipeline records unsorted metric values. You must report the largest gap between successive values once sorted — in linear time, because the array is huge and re-sorting is too slow.
Problem
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 run in linear time and use linear extra space.
Input
An integer array nums.
Output
An integer: the maximum successive gap in sorted order, or 0 if length < 2.
Constraints
- 1 <= nums.length <= 100000
- 0 <= nums[i] <= 10^9
- Linear time and linear space required (no comparison sort).
Examples
Example 1
Input
nums = [3,6,9,1]
Output
3
Sorted is [1,3,6,9]; successive gaps are 2,3,3 — max is 3.
Example 2
Input
nums = [10]
Output
0
Fewer than two elements.