Zepruv › Coding problems

Maximum Gap

Medium · Array, Sorting, Bucket Sort, Radix Sort

Given an integer array nums, return the maximum difference between two successive elements in its sorted form. If the array contains less 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

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

The array contains less than 2 elements, therefore return 0.

Constraints

Starter code (python)

class Solution(object):
    def maximumGap(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        

Related problems

Previous: Find Minimum in Rotated Sorted Array II · Next: Two Sum II - Input Array Is Sorted · All problems

Zepruv is a technical interview practice platform. Read any problem for free; sign in to run your code, submit and keep your progress.