Thanks to visit codestin.com
Credit goes to leetcode-py.wisl.dev

Skip to main content
LeetCode 164, Medium. Topics: Array, Sorting, Bucket Sort, Radix Sort, Pigeonhole Principle. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 18 parametrized pytest cases, and a playground notebook:

Problem

Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>. You must write an algorithm that runs in linear time and uses linear extra space.

Examples

Explanation: The sorted form of the array is [1,3,6,9], either (3,6) or (6,9) has the maximum difference 3.
Explanation: The array contains less than 2 elements, therefore return 0.

Constraints

  • 1 <= nums.length <= 10^5
  • 0 <= nums[i] <= 10^9
Follow up: Could you solve it without using any built-in sorting function?

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

Last modified on September 7, 2026