Dsa
Sliding Window
Easy
Max Fruits in Sliding Window
You are given an array of integers where each integer represents a type of fruit. Your goal is to find the maximum number of fruits you can collect from a contiguous segment of the array, where you can only collect at most 2 types of fruits at a time.
Your program should read the integer array from standard input and print the maximum number of fruits that can be collected based on the above conditions.
**Input Format:**
The first line will contain an integer n (1 ≤ n ≤ 10^6), the number of fruits.
The second line will contain n space-separated integers representing the types of fruits.
**Output Format:**
A single integer, the maximum number of fruits that can be collected.
**Example:**
Input:
7
1 2 1 2 3 1 1
Output:
5
The segment [1, 2, 1, 2, 3] has 5 fruits with types 1 and 2 only, and the segment [3, 1, 1] cannot be considered since type 3 exceeds the limit.
Key concepts
sliding_windowarrayhash_map
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now