#C14787. Longest Contiguous Subarray

    ID: 44474 Type: Default 1000ms 256MiB

Longest Contiguous Subarray

Longest Contiguous Subarray

Given T arrays of integers, your task is to determine, for each array, the length of the longest subarray that forms a contiguous set of integers. In other words, after sorting the array, find the maximum length segment where every two consecutive elements satisfy the condition $$a_{i+1} = a_i + 1$$. If the array is empty, the answer is 0.

Note: The subarray does not need to be a contiguous segment in the original array; it is defined after sorting the array.

inputFormat

The input is read from stdin and has the following format:

  1. The first line contains an integer T, the number of arrays.
  2. Then for each array, the first number is an integer N that denotes the number of elements in the array, followed by N space-separated integers.

For example:

3
5 1 3 2 5 4
0
7 1 5 3 4 2 8 10

outputFormat

For each array, output one integer representing the length of its longest contiguous subarray (when sorted), each on a new line, to stdout.

For example, the output corresponding to the sample input above could be:

5
0
5
## sample
1
0
0

</p>