Sliding Window Sum

MediumData EngineeringSliding WindowArray

Description

Given an array nums and a window size k (1 ≤ k ≤ nums.length), return the sum of each contiguous window of size k.

Examples

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

Summing each window of 2 consecutive values yields [3,5,7].

Input:nums = [5,5,5], k = 2
Output:[10,10]
Explanation:

Summing each window of 2 consecutive values yields [10,10].

Input:nums = [1,2,3], k = 3
Output:[6]
Explanation:

Summing each window of 3 consecutive values yields [6].

Constraints

  • 1 ≤ k ≤ nums.length ≤ 10⁴

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.