<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>

inline const auto optimize = []() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    return 0;
}();

class Solution {
public:
    static int maximumGap(const std::vector<int>& nums) {
        const int N = nums.size();

        // This is technically N*log(N), but let's see if it even works?
        const auto [mmin, mmax] = std::ranges::minmax(nums);
        const int inputSpan = mmax - mmin;
        if (inputSpan < 2) {
            return inputSpan;
        }

        // Now here comes the cool part. We divide these numbers into n-1
        // groups. Now the sum of all differences inside of the array, would
        // sum up to mmax-mmin. Inside of each bucket the max span can max be
        // (mmax-mmin)/(n-1). Since there are n numbers, but only n-1 buckets,
        // at least two buckets has to be occupied. This means that at least
        // one of gaps will be across buckets. All these gaps cannot be smaller
        // than (mmax-mmin)/(n-1) as that would mean that the total would be too
        // small. This means that either it's the answer or it's larger.
        const int bucketSize = std::max(1, (inputSpan) / (N - 1));
        const std::pair<int, int> SENTINEL{std::numeric_limits<int>::max(),
                                           std::numeric_limits<int>::min()};
        std::vector<std::pair<int, int>> buckets(N, SENTINEL);
        for (const auto x : nums) {
            const auto bucketNum = std::min(N - 1, (x - mmin) / bucketSize);
            buckets[bucketNum].first = std::min(buckets[bucketNum].first, x);
            buckets[bucketNum].second = std::max(buckets[bucketNum].second, x);
        }

        // ok, now then for each bucket
        int ans = std::numeric_limits<int>::min();
        int prevHigh = buckets[0].second;
        for (int i = 1; i < N; i++) {
            const auto& [bl, bh] = buckets[i];
            if (bh < bl)
                continue;

            ans = std::max(ans, bl - prevHigh);
            prevHigh = bh;
        }
        return ans;
    }
};
90106