#K91017. Longest Contiguous Subarray with Consecutive Integers

    ID: 37882 Type: Default 1000ms 256MiB

Longest Contiguous Subarray with Consecutive Integers

Longest Contiguous Subarray with Consecutive Integers

You are given an array of integers. Your task is to determine the length of the longest contiguous subarray such that when the subarray is sorted, the elements become consecutive integers. In other words, for a subarray a[l...r] with minimum value \(m\) and maximum value \(M\), the subarray is valid if \(M - m = r - l\). Note that if the subarray is empty, you should consider the result as 1.

Example:

Input: [10, 12, 11, 14, 13, 15]
Output: 6

This is because the entire array (after sorting becomes [10,11,12,13,14,15]) consists of consecutive integers.

inputFormat

The first line of input contains an integer \(T\) representing the number of test cases. Each test case follows:

  • The first line contains an integer \(n\) which is the number of elements in the array.
  • The second line contains \(n\) space-separated integers denoting the elements of the array.

outputFormat

For each test case, output one integer on a new line denoting the length of the longest contiguous subarray that, when sorted, forms a consecutive sequence of integers.

## sample
2
6
10 12 11 14 13 15
5
15 14 12 11 13
6

5

</p>