values. Imagine it sorted in ascending order. Return the largest difference between two neighbouring elements in that order.
If the array has fewer than two elements, return 0.
Sorting in O(n log n) solves it right away. Try to do it in linear time with linear extra space.values=[14, 2, 30, 7] → 16
Sorted: [2, 7, 14, 30]. The gaps are 5, 7 and 16, so the widest is 16.
values=[42] → 0
There are no neighbours, so the answer is 0.
values=[5, 5, 6, 5] → 1
values=[14, 2, 30, 7]
16
Run checks the first 3 tests in your browser. Submit checks all 126 tests on the server, hidden ones included.