#K62502. Fruit Harvest
Fruit Harvest
Fruit Harvest
You are given a sequence of fruits arranged in a row, represented by integers. Each integer denotes a type of fruit. You have two baskets, and each basket can only hold one type of fruit. Starting from any position, you want to collect fruits by moving rightward, but you can only collect fruits if there are at most two distinct types in your baskets.
Your task is to find the length of the longest contiguous segment of fruits from which you can collect fruits following this rule.
Formally, given a sequence of integers a1, a2, \dots, an, determine the maximum length L such that there exists some contiguous subarray ai, ai+1, \dots, ai+L-1 containing at most two distinct integers.
The mathematical condition is: \( |\{a_i, a_{i+1}, \dots, a_{i+L-1}\}| \le 2 \).
inputFormat
The input is given via stdin and has the following format:
n a1 a2 ... an
where:
n
is an integer representing the number of fruits.a1, a2, ..., an
are integers representing the types of fruits.
outputFormat
Output a single integer to stdout which is the length of the longest contiguous subarray containing at most two distinct types of fruits.
## sample3
1 2 1
3