Dsa
Dynamic Programming
Warm-up
Sequential Sum Slices
You have an array of integers. You need to find the maximum sum of any contiguous subarray of a fixed length k. If the length of the array is less than k, return -1.
Input format:
- The first line contains two integers, n (the number of elements) and k (the fixed length).
- The second line contains n integers representing the array elements.
Output format:
- A single integer representing the maximum sum of a contiguous subarray of length k.
Example:
Input:
5 3
1 2 3 4 5
Output:
12
Key concepts
dynamic_programmingsubarraycumulative_sum
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