Problem statement
Given a row of fruits where each value is a kind, find the longest stretch that holds at most two kinds. Two baskets are carried, and each basket takes one kind only.
The stretch is contiguous, so this is a window question with a limit on how many kinds the window may contain.
Examples
Input[1, 2, 1, 2, 3]
Output4
The first four fruits hold two kinds, and adding the 3 makes three.
Input[7, 7, 7]
Output3
One kind is fewer than two, so the whole row fits.
The walkthrough is part of Premium
The statement and the examples above stay free. The full how-to-solve walkthrough, the cost notes in plain words and the interactive step-by-step visualizer unlock with a Premium plan.
See pricing