<p>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>.</p>

<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [3,6,9,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The sorted form of the array is [1,3,6,9], either (3,6) or (6,9) has the maximum difference 3.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [10]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The array contains less than 2 elements, therefore return 0.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def maximumGap(self, nums: List[int]) -> int:
        if len(nums) < 2:
            return 0
        
        # Step 1: Find the minimum and maximum values in the array
        min_num = min(nums)
        max_num = max(nums)
        
        # If all elements are the same, the maximum gap is 0
        if min_num == max_num:
            return 0
        
        # Step 2: Calculate the gap size
        n = len(nums)
        gap = max(1, (max_num - min_num) // (n - 1))  # Maximum possible gap between buckets
        
        # Step 3: Initialize the buckets
        bucket_min = [float('inf')] * (n - 1)
        bucket_max = [float('-inf')] * (n - 1)
        bucket_used = [False] * (n - 1)
        
        # Step 4: Place each number into a bucket
        for num in nums:
            if num == min_num or num == max_num:
                continue
            # Calculate bucket index
            idx = (num - min_num) // gap
            # Ensure idx does not exceed bounds (idx should be in the range [0, n-2])
            idx = min(idx, n - 2)
            bucket_min[idx] = min(num, bucket_min[idx])
            bucket_max[idx] = max(num, bucket_max[idx])
            bucket_used[idx] = True
        
        # Step 5: Find the maximum gap
        max_gap = 0
        previous_max = min_num
        
        for i in range(n - 1):
            if not bucket_used[i]:
                continue
            max_gap = max(max_gap, bucket_min[i] - previous_max)
            previous_max = bucket_max[i]
        
        # Step 6: Compare with the max_num
        max_gap = max(max_gap, max_num - previous_max)
        
        return max_gap

31261