LeetCode 164: Maximum Gap Solution

Master LeetCode problem 164 (Maximum Gap), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

164. Maximum Gap

Problem Explanation

Explanation

To solve this problem in linear time and space, we can use the concept of bucket sort. The idea is to divide the range of the input numbers into N-1 buckets, where N is the number of elements in the input array. Then, we distribute the numbers into these buckets based on their value ranges. Finally, we calculate the maximum gap by comparing the minimum value of the next non-empty bucket with the maximum value of the previous non-empty bucket.

  1. Calculate the minimum and maximum values in the input array.
  2. Calculate the bucket size as (max - min) / (n - 1), where n is the number of elements in the input array.
  3. Initialize an array of buckets and distribute the numbers into the appropriate buckets.
  4. Iterate through the buckets to find the maximum gap between non-empty buckets.

Time Complexity: O(N)
Space Complexity: O(N)

Solution Code

class Solution {
    public int maximumGap(int[] nums) {
        if (nums == null || nums.length < 2) {
            return 0;
        }

        int n = nums.length;
        int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
        for (int num : nums) {
            min = Math.min(min, num);
            max = Math.max(max, num);
        }

        int bucketSize = Math.max(1, (max - min) / (n - 1));
        int numBuckets = (max - min) / bucketSize + 1;

        int[] minBucket = new int[numBuckets];
        Arrays.fill(minBucket, Integer.MAX_VALUE);
        int[] maxBucket = new int[numBuckets];
        Arrays.fill(maxBucket, Integer.MIN_VALUE);

        for (int num : nums) {
            int idx = (num - min) / bucketSize;
            minBucket[idx] = Math.min(minBucket[idx], num);
            maxBucket[idx] = Math.max(maxBucket[idx], num);
        }

        int maxGap = 0, prevMax = min;
        for (int i = 0; i < numBuckets; i++) {
            if (minBucket[i] == Integer.MAX_VALUE || maxBucket[i] == Integer.MIN_VALUE)
                continue;
            maxGap = Math.max(maxGap, minBucket[i] - prevMax);
            prevMax = maxBucket[i];
        }

        return maxGap;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 164 (Maximum Gap)?

This page provides optimized solutions for LeetCode problem 164 (Maximum Gap) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 164 (Maximum Gap)?

The time complexity for LeetCode 164 (Maximum Gap) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 164 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 164 in Java, C++, or Python.

Back to LeetCode Solutions