Dsa
Sliding Window
Medium
Max Consecutive Sum
Given an array of integers 'nums' and an integer 'k', find the maximum sum of any contiguous subarray of size 'k'. If 'k' is greater than the length of the array, return 0.
Input Format:
- The first line contains an integer 'n' (1 ≤ n ≤ 10^5), the number of elements in the array.
- The second line contains 'n' space-separated integers representing the elements of the array.
- The third line contains an integer 'k' (1 ≤ k ≤ n).
Output Format:
- Print a single integer which is the maximum sum of the contiguous subarray of size 'k'.
Example:
Input:
5
1 2 3 4 5
2
Output:
9
(This corresponds to the subarray [4, 5] which has a sum of 9.)
Key concepts
sliding_windowarraysum
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