#C356. Longest Subarray with At Most Two Distinct Integers
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 no more than two distinct integers.
In other words, you must identify a segment of the array (a subarray) where at most two unique numbers occur, and determine the maximum possible length of such a segment.
Note: If the input array is empty, the output should be 0.
Example: For the array [1, 1, 2, 3, 4], the longest contiguous subarray with at most two distinct integers is [1, 1, 2], and its length is 3.
inputFormat
The input is read from standard input (stdin) and consists of two lines:
1. The first line contains an integer n, representing the number of elements in the array.
2. The second line contains n space-separated integers representing the array.
outputFormat
Output a single integer to standard output (stdout) which is the length of the longest contiguous subarray containing at most two distinct integers.## sample
5
1 1 2 3 4
3