Fruit Basket Problem

Sliding Window Arrays Two Pointers

Fruit Basket Problem

You are given an array of characters where each character represents a type of fruit that grows on a tree. You have two baskets, and each basket can only hold one type of fruit. Starting at any tree in the array, you must pick exactly one fruit from every tree (moving to the right) until you encounter a third type of fruit. Write a function to return the maximum number of fruits you can pick using this rule.

Input

  • An array of characters (e.g., ['A', 'B', 'C', 'A', 'C']).

Output

  • An integer representing the maximum number of fruits you can collect in one contiguous section.

Example

Input: ['A', 'B', 'C', 'A', 'C']

Output: 3

Explanation: The longest contiguous subarray with at most two distinct fruits is ['C', 'A', 'C'].

Hint: Use the sliding window technique to efficiently manage the current range of trees while tracking the types of fruit in your baskets.