#K34722. Longest Subarray with At Most Two Distinct Integers

    ID: 25373 Type: Default 1000ms 256MiB

Longest Subarray with At Most Two Distinct Integers

Longest 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.

Formally, if the array is \(a_1, a_2, ..., a_n\), you need to find the maximum \(L\) such that there exists an index \(i\) where the subarray \(a_i, a_{i+1}, ..., a_{i+L-1}\) contains no more than two distinct integers.

This problem can be solved using the sliding window technique. The key observation is that if a window contains more than two distinct numbers, you can shrink it from the left until it becomes valid again.

Note: If the array is empty, the result is 0.

inputFormat

The first line of input contains an integer \(n\) (the number of elements in the array). The second line contains \(n\) space-separated integers representing the array.

outputFormat

Output a single integer representing the maximum length of a subarray that contains at most two distinct integers.

## sample
8
1 2 1 2 3 3 4 5
4