SDE Path

Fruits into Baskets

Medium

Fruits into Baskets

Given an array representing fruit types, find the longest subarray containing at most two distinct types.

Input format

The first line contains N. The second line contains N integers (fruit types 0–9).

Output format

Print the longest subarray with at most 2 distinct values.

Constraints

  • Values fit in a 64-bit signed integer
  • Trailing whitespace and a trailing newline are ignored by the judge

Read from stdin, write to stdout. Sample cases below show the exact format.

Sample cases

Example 1
Input
3
1 2 1
Expected output
3
Example 2
Input
3
0 1 2
Expected output
2