<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 {
public:
    int maximumGap(vector<int>& nums) {
        int b_min = INT_MAX;
        int b_max = INT_MIN;
        int i, index, pre;
        int ** bucket;
        int gap, max_gap=-1;
        if (nums.size() == 1) {
            return 0;
        }
        if (nums.size() == 2) {
            if (nums[0]-nums[1] > nums[1]-nums[0]) {
                return nums[0]-nums[1];
            }
            else {
                return nums[1]-nums[0];
            }
        }

        for (i=0;i<nums.size();i++) {
            if (nums[i]<b_min) {
                b_min = nums[i];
            }
            if (nums[i]>b_max) {
                b_max = nums[i];
            }
        }

        bucket = new int *[nums.size()-1];
        for (i=0;i<nums.size()-1;i++) {
            bucket[i] = new int [2];
            if (i==0) {
                bucket[i][0] = b_min;
                bucket[i][1] = b_min;
            }
            else if (i == nums.size()-2) {
                bucket[i][0] = b_max;
                bucket[i][1] = b_max;
            }
            else {
                bucket[i][0] = INT_MAX;
                bucket[i][1] = INT_MIN;
            }
        }
        gap = (int)ceil((double(b_max) - double(b_min)) / double(nums.size()-1));

        for (i=0;i<nums.size();i++) {
            if (nums[i]==b_min || nums[i]==b_max) {
                continue;
            }
            else {
                index = (nums[i]-b_min)/gap;
                if (nums[i] < bucket[index][0]) {
                    bucket[index][0] = nums[i];
                }
                if (nums[i] > bucket[index][1]) {
                    bucket[index][1] = nums[i];
                }
            }
        }

        /*
        for (i=0;i<nums.size()-1;i++) {
            cout<<bucket[i][0]<<" "<<bucket[i][1]<<endl;
        }
        */
        pre = b_min;
        for (i=0;i<nums.size()-1;i++) {
            if (bucket[i][0] == INT_MAX || bucket[i][1] == INT_MIN) {
                continue;
            }
            if (bucket[i][0]-pre > max_gap) {
                max_gap = bucket[i][0]-pre;
            }
            pre = bucket[i][1];
        }
        return max_gap;
    }
};
108444