Maximum Subarray of Size K - aloalgo

Maximum Subarray of Size K

Easy

You are given an array of integers nums and a positive integer k.

Your task is to find the maximum sum of any contiguous subarray of size exactly k.

Note that:

  • A subarray is a contiguous part of an array.

Example 1

Inputs
nums = [2, 1, 5, 1, 3, 2]
k = 3
Output
9
Explanation:

The subarray [5, 1, 3] has the maximum sum of 9.

Example 2

Inputs
nums = [2, 3, 4, 1, 5]
k = 2
Output
7
Explanation:

The subarray [2, 3] or [3, 4] both have a sum of 7, but there's no subarray of size 2 with a larger sum.

Example 3

Inputs
nums = [-1, -2, -3, -4]
k = 2
Output
-3
Explanation:

The subarray [-1, -2] has the maximum sum of -3.

Loading...
Inputs
nums = [2, 1, 5, 1, 3, 2]
k = 3
Output
9

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!