#C5872. Maximizing Consecutive Fruit Stalls
Maximizing Consecutive Fruit Stalls
Maximizing Consecutive Fruit Stalls
You are given a sequence of fruit stalls, where each stall is represented by an integer that denotes the type of fruit available. You may visit a contiguous sequence of stalls and collect fruits, but you are restricted to collecting fruits from at most two distinct stall types.
Your task is to determine the maximum number of consecutive stalls you can visit while ensuring that there are no more than two distinct types of fruits among those stalls.
Formally, given an array \(a_1, a_2, \ldots, a_n\), find the maximum length \(L\) such that there exists an index \(i\) where the subarray \(a_i, a_{i+1}, \ldots, a_{i+L-1}\) contains at most two distinct integers.
inputFormat
The input is provided via standard input (stdin). The first line contains an integer (n), representing the number of fruit stalls. The second line contains (n) space-separated integers, each denoting the type of fruit available at the corresponding stall. If (n = 0), there are no stalls.
outputFormat
Output a single integer to standard output (stdout), representing the maximum number of consecutive stalls you can visit containing at most two distinct fruit types.## sample
8
1 2 1 2 3 2 2 1
4