#164
Maximum Gap
specialist · 855 · lc medium +31 · verified · 51.5% accepted · 3,547 likes · top 41%
Description
Given an integer array nums, return the largest difference between any two successive elements when the array is sorted. Return 0 if nums has fewer than two elements.
Your solution must run in linear time and use linear extra space.
Example 1:
Input: nums = [3,6,9,1]
Output: 3
Explanation: The sorted form of the array is [1,3,6,9], either (3,6) or (6,9) has the maximum difference 3.
Example 2:
Input: nums = [10]
Output: 0
Explanation: The array contains less than 2 elements, therefore return 0.
Code
1
2
3