#t146. 最长连续段

最长连续段

Longest Consecutive Segment

Problem Description

For an array [b1,b2,…,bk][b_1, b_2, \ldots, b_k] of kk integers, if bi+1=bi+1b_{i+1} = b_i + 1 holds for all 1≤i<k1 \le i < k, then we call the array bb a consecutive segment.

You are given an array [a1,a2,…,an][a_1, a_2, \ldots, a_n] of nn integers. You may rearrange the elements of aa in any order. After rearranging, among all subarrays of aa that are consecutive segments, what is the length of the longest one?

For example, for the array [1,0,2,4][1, 0, 2, 4], it can be rearranged as [4,0,1,2][4, 0, 1, 2], which has the following 1010 subarrays: $[4], [0], [1], [2], [4, 0], [0, 1], [1, 2], [4, 0, 1], [0, 1, 2], [4, 0, 1, 2]$. Among them, all except [4,0],[4,0,1],[4,0,1,2][4, 0], [4, 0, 1], [4, 0, 1, 2] are consecutive segments, so the longest subarray that is a consecutive segment has length 33.

Input Format

The first line contains a positive integer nn, the length of the array.

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n, the elements of the array.

Output Format

One line containing one integer: the length of the longest subarray that is a consecutive segment after rearranging aa.

Sample Input 1

4
1 0 2 4

Sample Input 2

9
9 9 8 2 4 4 3 5 3

Sample Output 1

3

Sample Output 2

4

Sample Explanation

For the first sample, one possible rearrangement of [1,0,2,4][1, 0, 2, 4] is [4,0,1,2][4, 0, 1, 2]. In this permutation the subarray [0,1,2][0, 1, 2] satisfies that each number is exactly 11 greater than the previous, so it is a consecutive segment of length 33. It can also be arranged as [0,1,2,4][0, 1, 2, 4], where the consecutive segment [0,1,2][0, 1, 2] of length 33 still exists. No consecutive segment longer than 33 can be obtained, so the answer is 33.

Constraints

For 40%40\% of the test cases, 1≤n≤81 \le n \le 8 is guaranteed.

For all test cases, 1≤n≤1051 \le n \le 10^5 and −109≤ai≤109-10^9 \le a_i \le 10^9 are guaranteed.