Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Maximum Gap

XPChallenge Points: 20
levelLevel: Medium

Given an integer array nums, find the largest gap between any two consecutive numbers once the array is sorted in ascending order.
If nums has fewer than two numbers, return 0.
Your solution should run in linear time and use only linear extra space.

Example 1:

Input: nums = [3,6,9,1]

Output: 3

Explanation:

Sorted array → [1,3,6,9] The consecutive differences are: 3-1 = 2, 6-3 = 3, 9-6 = 3. The largest gap is 3.

Example 2:

Input: nums = [10]

Output: 0

Explanation:

There is only one number, so no gap can be calculated.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result