Dsa
Sliding Window
Easy
Maximum Fruit Harvest
You are a farmer collecting fruits from a row of trees. Each tree can produce a certain number of fruits, and you want to collect fruits from a continuous segment of trees. Given an array of integers representing the number of fruits each tree produces, find the maximum number of fruits you can collect from any continuous segment of trees of size 'k'.
Input:
- The first line contains two integers, n (the number of trees) and k (the size of the segment).
- The second line contains n integers, each representing the fruits from a tree.
Output:
- Output a single integer, the maximum number of fruits collectible from a segment of size k.
Example:
Input:
6 3
1 2 3 4 5 6
Output:
15
Key concepts
sliding_windowarraysgreedy
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