Back to course home
0% completed
Solution: Maximum Gap
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]
.....
.....
.....
Like the course? Get enrolled and start learning!
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible