Given an integer array nums, return the maximum difference between two successive elements in its sorted form. If the array contains less than two elements, return 0.
You must write an algorithm that runs in linear time and uses linear extra space.
Method 2 - Using bucketsort and pigeonhole principle#
Using counting sort or radix sort can be effective if the integer range is known and small relative to the size of the array, but these methods are impractical for unknown or very large ranges.
By considering that an array of n numbers in a contiguous sequence has a maximum gap of one, we can infer that real-world scenarios with missing numbers create gaps. Identifying these missing ranges efficiently allows us to solve this problem in O(n) time.
The strategy is to divide the values between the given array’s minimum and maximum values into equally-sized buckets. The goal is to ensure that at least one bucket remains empty. An empty bucket indicates a gap equal to the difference between the maximum value of the previous non-empty bucket and the minimum value of the next non-empty bucket. This is based on the Pigeonhole Principle, which asserts that placing n-1 numbers into n buckets guarantees at least one empty bucket.
Below is a linear-time algorithm for computing the maximum gap, enabling constant time computation:
Given a set S of n > 2 real numbers x1, x2, ..., xn:
If the length of the array is less than 2, return 0.
Find the maximum, x_max, and the minimum, x_min, in S.
Divide the interval [x_min, x_max] into (n-1) “buckets” of equal size δ = (x_max - x_min) / (n-1).
For each of the remaining n-2 numbers, determine in which bucket it falls using the floor function. The number xi belongs to the k-th bucket Bk if, and only if, ⌊(xi - x_min) / δ⌋ = k-1.
For each bucket Bk, compute xk_min and xk_max among the numbers that fall in Bk. If the bucket is empty, return nothing. If the bucket contains only one number, return that number as both xk_min and xk_max.
Construct a list L of all the ordered minima and maxima: L: (x1_min, x1_max), (x2_min, x2_max), ..., (x(n-1)_min, x(n-1)_max).
Note: Since there are n-1 buckets and only n-2 numbers, by the Pigeonhole Principle, at least one bucket must be empty. Therefore, the maximum distance between a pair of consecutive points must be at least the length of the bucket. Consequently, the solution is not found among a pair of points that are contained in the same bucket.
In L, find the maximum distance between a pair of consecutive minimum and maximum (xi_max, xj_min), where j > i.
nums =[5,3,1,8,9,2,4], n =71. Min =1, Max =92. Create 6buckets(7-1)with equal size δ =(9-1)/(7-1)=1.333. Place the remaining elements into the appropriate buckets using the formula(nums[i]- Min)/δ. Calculate min and max for each bucket: b0 b1 b2 b3 b4 b5
____________________________________
|_2___|__3__|__4__|__5__|______|__8__|^^^^^^^12.333.6656.337.669 For example,for nums[0]=5, the bucket number =(nums[0]- Min)/δ =(5-1)/1.33=3=> b3
Note: b4 is empty.4. Identify empty buckets(gaps) and compute the gap value as the difference between the max of the previous non-empty bucket and the min of the next non-empty bucket. For instance, b4 is empty, the previous non-empty bucket is b3, and the next non-empty bucket is b5. Thus, the gap value at b4 = min(b5)- max(b3)=8-5=3.5. Update a global maximum and iterate over all empty buckets to perform the gap calculation. Since there is one empty bucket, the maximum gap is3.