#t146. 最长连续段
最长连续段
Longest Consecutive Segment
Problem Description
For an array of integers, if holds for all , then we call the array a consecutive segment.
You are given an array of integers. You may rearrange the elements of in any order. After rearranging, among all subarrays of that are consecutive segments, what is the length of the longest one?
For example, for the array , it can be rearranged as , which has the following 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 are consecutive segments, so the longest subarray that is a consecutive segment has length .
Input Format
The first line contains a positive integer , the length of the array.
The second line contains integers , 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 .
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 is . In this permutation the subarray satisfies that each number is exactly greater than the previous, so it is a consecutive segment of length . It can also be arranged as , where the consecutive segment of length still exists. No consecutive segment longer than can be obtained, so the answer is .
Constraints
For of the test cases, is guaranteed.
For all test cases, and are guaranteed.