#K78572. Longest Subarray with At Most Two Distinct Integers

    ID: 35116 Type: Default 1000ms 256MiB

Longest Subarray with At Most Two Distinct Integers

Longest Subarray with At Most Two Distinct Integers

Given an array of integers, your task is to determine the length of the longest contiguous subarray that contains at most two distinct integers. This problem can be solved efficiently using a sliding window approach.

Explanation: Suppose you have an array \(A\) of length \(n\). You need to find the maximum length \(L\) such that there exists some \(i\) and \(j\) (with \(0 \le i \le j < n\)) where the subarray \(A[i \dots j]\) contains at most two distinct numbers. If the array is empty, the answer is 0.

Example:

Input: 7\n       4 3 5 5 6 2 2
Output: 3

inputFormat

The input is read from standard input (stdin) and consists of two lines. The first line contains a single integer n which represents the number of elements in the array. If n is 0 then no further input is given. If n > 0, the second line contains n space-separated integers representing the elements of the array.

outputFormat

Output to standard output (stdout) a single integer representing the length of the longest contiguous subarray that contains at most two distinct integers.

## sample
7
4 3 5 5 6 2 2
3