Given an array of n
non-negative integers, find the maximum difference between 2 consecutive numbers in the sorted array of the given integers.
{
"arr": [1, 3, 7, 2]
}
Output:
4
We can see that in sorted order, the array becomes [1, 2, 3, 7], and the maximum difference is 7 – 3 = 4.
{
"arr": [1, 1, 1]
}
Output:
0
Here the sorted array is [1, 1, 1], and so the maximum difference is 1 – 1 = 0.
n
* log(n)
. You can use up-to linear extra space.Constraints:
n
<= 106We have provided one solution for this problem.
min
and max
respectively.max
– min
+ 1.buckets
of this size and use it to keep frequency of occurrence of numbers in an array.buckets[arr[i] – max]
by 1.buckets
array. That is the answer.O(n + range).
O(range).
O(n + range).
Input takes O(n) space, and we use O(range) of auxiliary space.
/*
Asymptotic complexity in terms of `n` = length of the given array `arr` and
`range` = max(Elements of array) – min(Elements of array):
* Time: O(n + range).
* Auxiliary space: O(range).
* Total space: O(n + range).
*/
static Integer maximum_gap(ArrayList<Integer> arr) {
int n = arr.size();
if(n==1) {
return 0;
}
int max = 0;
int min = 1000001;
// Find maximum and minimum elements.
for(int i = 0; i<n; i++) {
min = Math.min(min,arr.get(i));
max = Math.max(max,arr.get(i));
}
// Create array of size range to store the frequencies of the array elements.
int range = max - min + 1;
int buckets[] = new int[range + 5];
for(int i : arr) {
buckets[i - min]++;
}
// Finding maximum number of consecutive zeros between 2 non zero elements of buckets array.
int ans = 0;
for(int i = range-1; i>0;) {
int cnt = 0, temp = i;
i--;
while(i>=0 && buckets[i]==0) {
cnt++;
i--;
}
if(i>=0) {
ans = Math.max(ans, temp - i);
}
}
return ans;
}
We hope that these solutions to maximum gap have helped you level up your coding skills. You can expect problems like these at top tech companies like Amazon and Google.
If you are preparing for a tech interview at FAANG or any other Tier-1 tech company, register for Interview Kickstart’s FREE webinar to understand the best way to prepare.
Interview Kickstart offers interview preparation courses taught by FAANG+ tech leads and seasoned hiring managers. Our programs include a comprehensive curriculum, unmatched teaching methods, and career coaching to help you nail your next tech interview.
We offer 18 interview preparation courses, each tailored to a specific engineering domain or role, including the most in-demand and highest-paying domains and roles, such as:
â€To learn more, register for the FREE webinar.
The 11 Neural “Power Patterns” For Solving Any FAANG Interview Problem 12.5X Faster Than 99.8% OF Applicants
The 2 “Magic Questions” That Reveal Whether You’re Good Enough To Receive A Lucrative Big Tech Offer
The “Instant Income Multiplier” That 2-3X’s Your Current Tech Salary
The 11 Neural “Power Patterns” For Solving Any FAANG Interview Problem 12.5X Faster Than 99.8% OF Applicants
The 2 “Magic Questions” That Reveal Whether You’re Good Enough To Receive A Lucrative Big Tech Offer
The “Instant Income Multiplier” That 2-3X’s Your Current Tech Salary
Just drop your name and email so we can send your Power Patterns PDF straight to your inbox. No Spam!
By sharing your contact details, you agree to our privacy policy.
Time Zone: Asia/Dhaka
We’ve sent the Power Patterns PDF to your inbox — it should arrive in the next 30 seconds.
📩 Can’t find it? Check your promotions or spam folder — and mark us as safe so you don’t miss future insights.
We’re hosting a private session where FAANG insiders walk through how they actually use these Power Patterns to crack interviews — and what sets top performers apart.
🎯 If you liked the PDF, you’ll love what we’re sharing next.
Time Zone: