#K14551. Longest Subarray with At Most Two Distinct Numbers

    ID: 24159 Type: Default 1000ms 256MiB

Longest Subarray with At Most Two Distinct Numbers

Longest Subarray with At Most Two Distinct Numbers

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 numbers. In other words, if (S) is any contiguous subarray, then the subarray is valid if (|{ x : x \in S }| \leq 2).

For example, given the array [1, 2, 1, 2, 3, 3], the longest valid subarray is [1, 2, 1, 2] which has a length of 4.

Your program should read input from standard input (stdin) and output the answer to standard output (stdout).

inputFormat

The first line of input contains an integer (n) representing the number of elements in the array. If (n = 0), the array is empty. The second line contains (n) space-separated integers representing the array elements.

outputFormat

Output a single integer: the length of the longest contiguous subarray with at most two distinct numbers.## sample

6
1 2 1 2 3 3
4