#K34172. Longest Subarray with At Most Two Distinct Integers

    ID: 25251 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. In other words, if the subarray has more than two unique numbers, it is not valid.

Example:

  • For the array [1, 2, 1, 3, 4, 2, 3], the longest subarray with at most two distinct integers is [1, 2, 1] and its length is 3.
  • For the array [1, 2, 2, 1, 3], the longest subarray with at most two distinct integers is [1, 2, 2, 1] and its length is 4.

The solution should be implemented reading from standard input (stdin) and outputting the result to standard output (stdout). If the array is empty, output 0.

inputFormat

The first line contains a single integer N, representing the number of elements in the array. The second line contains N space-separated integers representing the elements of the array.

outputFormat

Output a single integer: the length of the longest contiguous subarray that contains at most two distinct integers.## sample

7
1 2 1 3 4 2 3
3

</p>