Grokking the Engineering Manager Coding Interview
Ask Author
Back to course home

0% completed

Vote For New Content
Maximum Gap (medium)
On this page

Problem Statement

Examples

Try it yourself

Problem Statement

Given an integer array nums, return the largest difference between any two consecutive elements in the sorted form of nums. If the array has less than two elements, return 0.

Note: Your solution should run in linear time and use linear extra space.

Examples

Example 1:

  • Input: nums = [10, 50, 20, 90, 60]
  • Expected Output: 30
  • Justification: When sorted, the array becomes [10, 20, 50, 60, 90]. The largest gap is between 20 and 50 or 60 and 90, which is 30.

Example 2:

  • Input: nums = [5, 100, 1, 50, 9]
  • Expected Output: 50
  • Justification: The sorted array is [1, 5, 9, 50, 100]. The largest gap is between 50 and 100, which is 50.

Example 3:

  • Input: nums = [300, 10, 200, 100, 600]
  • Expected Output: 300
  • Justification: After sorting, the array becomes [10, 100, 200, 300, 600]. The largest gap is between 300 and 600, which is 300.

Constraints:

  • 1 <= nums.length <= 10<sup>5</sup>
  • 0 <= nums[i] <= 10<sup>9</sup>

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Like the course? Get enrolled and start learning!

On this page

Problem Statement

Examples

Try it yourself