Dsa
Sliding Window
Medium
Max Sum of Subarray with Constraints
Given an integer array `arr` and an integer `k`, find the maximum sum of any subarray of size at most `k`. A subarray is a contiguous part of an array. If the array is empty, return 0.
Input Format:
- The first line contains two integers, n (1 <= n <= 10^5) and k (1 <= k <= n).
- The second line contains n space-separated integers representing the elements of the array, where each integer is in the range [1, 10^4].
Output Format:
- Print the maximum sum of any subarray of size at most `k`.
Example:
Input:
5 3
1 2 3 4 5
Output:
12
Key concepts
sliding_windowarrayssubarrays
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