<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>
class Solution {
public:
    pair<int, int> find_max_min(vector<int> input) {
        int max = input[0], min = input[0];

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

            if (input[i] < min) {
                min = input[i];
            }
        }

        return pair<int, int>(max, min);
    }

    int linear_time_complexity(vector<int> num_vector) {
        pair<int, int> max_min = find_max_min(num_vector);

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

        double len =  (double)(max_min.first - max_min.second) / (double)(num_vector.size() - 1);
        
        vector<tuple<int, int, bool>> num_box(
            num_vector.size(),
            make_tuple(max_min.second, max_min.first, false));

        for (int i = 0; i < num_vector.size(); i++) 
        {
            int a = (num_vector[i] - max_min.second) / len;

            if (a >= num_box.size()){
               a--;
            };

            auto& box = num_box[a]; 

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

        int max_gap = 0;
        int max = get<0>(num_box[1]);
        int min = get<0>(num_box[0]);
        for (int i = 1; i < num_box.size() ; i++) {

           if(!get<2>(num_box[i]))continue;


            max = get<1>(num_box[i]);
            int new_gap = max - min;
            min = get<0>(num_box[i]);
            if (new_gap > max_gap) {
                max_gap = new_gap;
            }
        }

        return max_gap;
    }

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

        return linear_time_complexity(nums);
    }
};
99886