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

#include <tuple>
#include <vector>
using namespace std;

class Solution {
public:
    pair<int, int> findMaxAndMin(vector<int> values) {
        int maxValue = values[0];
        int minValue = values[0];

        for (int i = 0; i < values.size(); i++) {
            if (values[i] > maxValue) {
                maxValue = values[i];
            }

            if (values[i] < minValue) {
                minValue = values[i];
            }
        }

        return pair<int, int>(maxValue, minValue);
    }

    int calculateLinearTimeComplexity(vector<int> values) {
        pair<int, int> maxMinValues = findMaxAndMin(values);

        if (maxMinValues.first == maxMinValues.second) return 0;

        double interval = (double)(maxMinValues.first - maxMinValues.second) / (double)(values.size() - 1);

        vector<tuple<int, int, bool>> buckets(
            values.size(),
            make_tuple(maxMinValues.second, maxMinValues.first, false)
        );

        for (int i = 0; i < values.size(); i++) {
            int bucketIndex = (values[i] - maxMinValues.second) / interval;

            if (bucketIndex >= buckets.size()) {
                bucketIndex--;
            }

            auto& bucket = buckets[bucketIndex];

            if (values[i] >= get<0>(bucket)) {
                get<0>(bucket) = values[i];
                get<2>(bucket) = true;
            }
            if (values[i] <= get<1>(bucket)) {
                get<1>(bucket) = values[i];
                get<2>(bucket) = true;
            }
        }

        int maxGap = 0;
        int currentMax = get<0>(buckets[1]);
        int currentMin = get<0>(buckets[0]);

        for (int i = 1; i < buckets.size(); i++) {
            if (!get<2>(buckets[i])) continue;

            currentMax = get<1>(buckets[i]);
            int gap = currentMax - currentMin;
            currentMin = get<0>(buckets[i]);

            if (gap > maxGap) {
                maxGap = gap;
            }
        }

        return maxGap;
    }

    int maximumGap(vector<int>& nums) {
        if (nums.size() < 2)
            return 0;

        return calculateLinearTimeComplexity(nums);
    }
};

99886