<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_2:
    def maximumGap(self, nums):
        # if (nums is None or len(nums) <= 1): return 0
        if (not nums or len(nums) <= 1): return 0
        # assert len(nums) >= 2
        nums.sort() # O(n log n)
        maxGap = nums[1] - nums[0]
        for i in range(1, len(nums) - 1):
            maxGap = max(nums[i + 1] - nums[i], maxGap)
        return maxGap


class Solution_3:
    def maximumGap(self, nums):
        if not nums or len(nums) <= 1:
            return 0

        n = len(nums)
        min_val, max_val = min(nums), max(nums)
        gap = (max_val - min_val) // (n - 1)
        if gap == 0:
            return max(nums) - min(nums)

        buckets = [[None, None] for _ in range(n - 1)]  # [min, max] for each bucket

        for num in nums:
            if num == min_val or num == max_val:
                continue
            i = (num - min_val) // gap
            buckets[i][0] = min(buckets[i][0], num) if buckets[i][0] else num
            buckets[i][1] = max(buckets[i][1], num) if buckets[i][1] else num

        max_gap = gap
        prev_max = min_val
        for i in range(n - 1):
            if buckets[i][0] is not None:
                max_gap = max(max_gap, buckets[i][0] - prev_max)
                prev_max = buckets[i][1]

        max_gap = max(max_gap, max_val - prev_max)
        return max_gap


from math import inf

class Solution:
    def maximumGap(self, nums):
        # Get the length of the list
        num_elements = len(nums)

        # If there are less than 2 elements, the maximum gap is 0
        if num_elements < 2:
            return 0

        # Calculate minimum and maximum value in the list
        min_val, max_val = min(nums), max(nums)

        # Calculate the bucket size, it should be at least 1
        bucket_size = max(1, (max_val - min_val) // (num_elements - 1))
      
        # Calculate the number of buckets needed
        bucket_count = (max_val - min_val) // bucket_size + 1
      
        # Create buckets, each bucket with a pair of values: [min_val_of_bucket, max_val_of_bucket]
        buckets = [[inf, -inf] for _ in range(bucket_count)]
      
        # Place each number in its respective bucket
        for value in nums:
            bucket_index = (value - min_val) // bucket_size
            buckets[bucket_index][0] = min(buckets[bucket_index][0], value)
            buckets[bucket_index][1] = max(buckets[bucket_index][1], value)
          
        # Initialize the answer variable that will store the maximum gap
        max_gap = 0

        # Initialize the variable that keeps track of the previous bucket's maximum value
        previous_max = inf

        # Iterate through the buckets to find the maximum gap
        for current_min, current_max in buckets:
            # Skip the bucket if it is empty
            if current_min > current_max:
                continue

            # The gap is the difference between the current bucket's min and the previous bucket's max
            max_gap = max(max_gap, current_min - previous_max)
          
            # Update the previous bucket max for the next iteration
            previous_max = current_max
      
        # Return the maximum gap found
        return max_gap

# Example usage:
# sol = Solution()
# print(sol.maximumGap([3, 6, 9, 1])) # Output: 3

35618