#K86607. Longest Subarray with Two Distinct Integers

    ID: 36902 Type: Default 1000ms 256MiB

Longest Subarray with Two Distinct Integers

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

The problem can be formalized as follows:

Let \(A = [a_1, a_2, \dots, a_n]\) be the array. Determine the maximum length \(L\) such that there exists a subarray \(A[i \dots j]\) (with \(1 \le i \le j \le n\)) containing at most two distinct integers. In other words, if \(D\) represents the set of distinct integers in the subarray, then \(|D| \leq 2\).

This problem tests your understanding of the sliding window technique and handling subarrays with constraints.

inputFormat

The first line contains a single integer \(n\) (\(0 \le n \le 10^5\)), representing the number of elements in the array.

The second line contains \(n\) space-separated integers, where each integer \(a_i\) (\(|a_i| \leq 10^9\)) is an element of the array.

outputFormat

Output a single integer, which is the length of the longest contiguous subarray that contains at most two distinct integers.

## sample
5
1 2 1 2 3
4

</p>