Dsa
Arrays
Easy
Max Consecutive Fruits
You are at a fruit market and can only buy up to k different types of fruits. Given an array representing the sequence of fruits you pick in order, return the maximum number of fruits you can collect consecutively.
Input:
The first line contains two integers n (1 <= n <= 10^5) and k (1 <= k <= n), the length of the array and the limit of different types of fruits.
The second line contains n space-separated integers representing the fruit types.
Output:
Return the maximum number of consecutive fruits you can collect.
Example:
Input:
7 2
1 2 1 2 3 1 2
Output:
5
Key concepts
arrayssliding windowsubarray
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