Dsa
Two Pointers
Medium
Subarray with Budget
Given a list of positive integers representing costs of items and a total budget, find the length of the longest contiguous subarray of items that can be purchased without exceeding the budget.
Input: The first line contains an integer n (1 <= n <= 100,000), the number of items. The second line contains n space-separated integers representing the costs of the items. The third line contains an integer budget (1 <= budget <= 1,000,000).
Output: Print a single integer representing the length of the longest valid subarray.
Example:
Input:
6
1 2 3 4 5 6
10
Output:
4
Key concepts
two_pointerssliding_windowarrays
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