#K93172. Longest Subarray with At Most Two Distinct Numbers
Longest Subarray with At Most Two Distinct Numbers
Longest Subarray with At Most Two Distinct Numbers
Given a sequence of N positive integers, your task is to find the length of the longest contiguous subarray that contains at most two distinct numbers.
If N is 0, the sequence is empty and the answer should be 0.
The problem tests your ability to use a two-pointer (sliding window) technique in order to efficiently solve a subarray/substring problem using a frequency map or alternative methods.
In mathematical terms, if you have an array \(a_1, a_2, \ldots, a_N\), find the maximum \(L\) such that there exists indices \(i\) and \(j\) (with \(1 \leq i \leq j \leq N\)) satisfying \(j - i + 1 = L\) and the subarray \(a_i, a_{i+1}, \ldots, a_j\) contains at most two unique elements.
inputFormat
The first line contains a single integer N (\(0 \leq N \leq 10^5\)), representing the number of elements in the sequence.
If N is greater than 0, the second line contains N space-separated integers which represent the sequence.
outputFormat
Output a single integer -- the length of the longest contiguous subarray that contains at most two distinct numbers.
## sample7
1 2 1 2 3 1 3
4