All problems

Problem 211

Maximum Gap

Medium · 20 pts · Array, Sorting, Bucket Sort

Problem

You are given an array of N integers. Imagine it sorted in increasing order, and find the largest difference between two neighbouring elements in that sorted order. If the array has fewer than two elements, print 0. Sorting and scanning takes O(N log N) and is fast enough. A classic linear-time solution uses buckets: the maximum gap is at least (max - min) / (N - 1), so it never occurs inside a bucket of that width, only between buckets.

Input

First line: integer N. Second line: N space-separated integers.

Output

Print the maximum gap.

Constraints

1 <= N <= 100000; 0 <= A[i] <= 1000000000

Example input

4
3 6 9 1

Example output

3

Explanation

Sorting 3 6 9 1 gives 1 3 6 9. The gaps between neighbours are 2, 3 and 3, so the maximum gap is 3.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Longest Mountain in Array Next: Range Sum Queries