#K71967. Longest Contiguous Subarray With At Most Two Distinct Integers

    ID: 33649 Type: Default 1000ms 256MiB

Longest Contiguous Subarray With At Most Two Distinct Integers

Longest Contiguous Subarray With At Most Two Distinct Integers

You are given an array of integers. Your task is to find the length of the longest contiguous subarray that contains at most two distinct integers.

For example, consider the array \(A = [1, 2, 1, 2, 3, 4]\). The longest contiguous subarray that has at most two distinct integers is \([1, 2, 1, 2]\) which has length 4.

You are required to implement an efficient solution using a sliding window technique. The formal definition can be given using the formula:

[ L = \max_{0 \leq i \leq j < n} { j - i + 1 ;:; |{A[i], A[i+1], ..., A[j]}| \leq 2 }]

Note: The input is provided via stdin and the output should be printed to stdout.

inputFormat

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

For each test case, the first line contains an integer \(N\) representing the number of elements in the array. The second line contains \(N\) space-separated integers representing the array.

outputFormat

For each test case, output a single line containing the length of the longest contiguous subarray that has at most two distinct integers.

## sample
2
6
1 2 1 2 3 4
5
4 4 4 5 5
4

5

</p>