121. Maximum Gap

HardArrayArray

Given an integer array nums, return the maximum difference between two successive elements in its sorted form. If the array has fewer than two elements, return 0. Aim for linear time.

Input: An integer array.

Output: Integer — maximum successive gap.

Examples

Example 1
Input: [3,6,9,1]
Output: 3
Explanation: Sorted [1,3,6,9]; max gap is 3.
Example 2
Input: [10]
Output: 0
Explanation: Fewer than 2 elements.
Example 3
Input: [1,1,1]
Output: 0
Explanation: All equal.

Constraints

Asked by

MicrosoftAmazonGoogleMetaBloomberg
Solve this problem in the editor →