Sale
Summer Offer: $190/yr $99/yr
00 : 00 : 00 : 00
Get this deal
Backend Engineer Senior programming

Given an array of integers nums and an integer k, return the maximum sum of a subarray of size k. A subarray is a contiguous part of the array. You must solve it in O(n) time complexity.


Constraints:


  • 1 <= nums.length <= 2 * 10^4

  • -10^5 <= nums[i] <= 10^5

  • 1 <= k <= nums.length


Examples:


1. Input: nums = [2, 1, 5, 1, 3, 2], k = 3
Output: 9
Explanation: Subarray [5, 1, 3] has the maximum sum of 9.
2. Input: nums = [1, 2, 3, 4, 5], k = 2
Output: 9
Explanation: Subarray [4, 5] has the maximum sum of 9.
Suggested Answer

Trusted by 100+ professionals preparing for interviews

Trusted by 100+ professionals 50+ Company Question Banks 5+ Supported Languages

Practice More Questions Like This

Generate unlimited interview questions with structured answers, code runner, and AI-powered walkthroughs.

Get Started Free
Link copied to clipboard