Maximum Gap
Medium Sorting
nums is a list of whole numbers, none of them negative.
Put the numbers of nums in order from smallest to largest. A gap is the difference between two numbers next to each other in that order.
Return the widest gap, or 0 if nums has only one number.
Example 1
Input: nums = [14,2,30,9,21]
Output: 9
In order the numbers are 2, 9, 14, 21 and 30, and the gaps between neighbours are 7, 5, 7 and 9. The widest is 9, from 21 to 30.
Example 2
Input: nums = [100,100,7]
Output: 93
Example 3
Input: nums = [7,7,7]
Output: 0
Constraints
1 ≤ nums.length ≤ 2 × 10⁵0 ≤ nums[i] ≤ 10⁹
Solve it here
Write it in Python, Java or C++, run it on your own cases, and submit against hidden tests.
Practice this in an AI round
30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.
See the AI roundMore Sorting
- Group AnagramsAI round · Medium
- Contains DuplicateAI round · Easy
- How Many Numbers Are Smaller Than the Current NumberAI round · Very easy
- Valid AnagramEasy
- Majority Element IIMedium