Maximum Sum Subarray of Size K Problem
Maximum Sum Subarray of Size K Problem — ExecCode Easy DSA Practice
Solve the Maximum Sum Subarray of Size K problem on ExecCode. Free online easy DSA practice in Sliding Window. Write and run code in Java, C++, Python — no signup required to run.
Problem description
Given an array nums and an integer k, find the maximum sum among all contiguous subarrays of exactly size k. The window length never changes: first compute the sum of nums[0..k-1], then slide the window one step at a time by subtracting the outgoing left value and adding the incoming right value. This turns a repeated O(k) sum into an O(1) update per window.
Examples
Input nums = [2, 1, 5, 1, 3, 2, 4, 6], k = 3; Output 12. Input nums = [5, 2, -1, 0, 3], k = 2; Output 7. Input nums = [4, 4, 4], k = 3; Output 12
Constraints
1 ≤ k ≤ nums.length ≤ 10⁵ -10⁴ ≤ nums[i] ≤ 10⁴
Practice Maximum Sum Subarray of Size K free on ExecCode. Browse DSA problems, topic map, and placement guides.