Skip to content
BytePatterns

Maximum Gap Buckets

HardSorting#bucket-sort#pigeonhole~40m

Problem

Given a list of integers, return the largest difference between two values that would be adjacent if the list were sorted. Return 0 when fewer than two distinct values exist. Aim for a solution that does not sort the values.

Examples

Input:  nums = [3, 6, 9, 1]
Output: 3
Why:    sorted the values read 1, 3, 6, 9 and every step is 2 or 3
Input:  nums = [1, 10000000]
Output: 9999999
Why:    two values far apart, so the single gap is the answer
Input:  nums = [1, 1, 1]
Output: 0
Why:    edge case, no two distinct values exist

Hints

0 / 3

Stuck on the idea rather than the code? Counting Sort covers it.