BackeasySliding WindowGoogleAmazon

Network Protocol Extractor 39 Solution

Problem Statement

Network Protocol Extractor 39

You are given an array of integers and a positive integer k representing the size of a sliding window. For every contiguous subsequence of length k in the array, compute the sum of its elements. The task is to output the sums for all such windows in the order they appear.

Input format:

  • The first line contains two integers n and k (1 ≤ k ≤ n ≤ 10^5), where n is the number of elements in the array.
  • The second line contains n integers a_1, a_2, …, a_n, each satisfying -10^9 ≤ a_i ≤ 10^9.

Output format:

  • Print n - k + 1 integers, each representing the sum of a window of size k. The sums should be printed in the order of the windows, separated by single spaces.

The solution should run in linear time and use constant additional space beyond the input array.

Example 1
Input
5 3 1 2 3 4 5
Output
6 9 12

Explanation: The windows of size 3 are: - [1,2,3] → 1+2+3 = 6 - [2,3,4] → 2+3+4 = 9 - [3,4,5] → 3+4+5 = 12 Thus the output is 6 9 12.

Example 2
Input
4 2 10 -2 3 5
Output
8 1 8

Explanation: Windows: - [10,-2] → 10+(-2) = 8 - [-2,3] → -2+3 = 1 - [3,5] → 3+5 = 8 Output: 8 1 8.

Example 3
Input
4 1 0 0 0 0
Output
0 0 0 0

Explanation: With window size 1, each element is its own sum. All sums are 0.

Example 4
Input
4 3 -1 -2 -3 -4
Output
-6 -9

Explanation: Windows: - [-1,-2,-3] → -1-2-3 = -6 - [-2,-3,-4] → -2-3-4 = -9 Output: -6 -9.

Constraints

  • 1 <= n <= 100000
  • 1 <= k <= n
  • -1000000000 <= a_i <= 1000000000
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Network Protocol Extractor 39 — Problem Statement & Solution Guide

Sliding WindowEasyBitmasking
TimeO(n)
|
SpaceO(1)

Problem Description

Network Protocol Extractor 39

You are given an array of integers and a positive integer k representing the size of a sliding window. For every contiguous subsequence of length k in the array, compute the sum of its elements. The task is to output the sums for all such windows in the order they appear.

Input format:

- The first line contains two integers n and k (1 ≤ k ≤ n ≤ 10^5), where n is the number of elements in the array.

- The second line contains n integers a_1, a_2, …, a_n, each satisfying -10^9 ≤ a_i ≤ 10^9.

Output format:

- Print n - k + 1 integers, each representing the sum of a window of size k. The sums should be printed in the order of the windows, separated by single spaces.

The solution should run in linear time and use constant additional space beyond the input array.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Network Protocol Extractor 39"

easy

WHY DOES IT MATTER?

Sliding windows turn quadratic work into linear work for many contiguous‑segment problems.

OPTIMIZATION CHALLENGE

The key is to avoid recomputing overlapping portions of consecutive windows.

REAL-WORLD CONNECTION

Network monitors compute moving averages of packet rates using the same incremental logic.

Initialize the first window sum once, then loop once, updating the sum in place for each shift.

COMPLEXITY AT A GLANCE

⏱ Time:O(n)
💾 Space:O(1)

Core Theory — Why This Approach?

The sliding window technique transforms a naïve O(n·k) summation into a linear scan by reusing the previous window's computation. When moving the window one step to the right, the element exiting the window is subtracted and the new entering element is added, preserving the sum in O(1) time per shift.

Naïve approaches recompute each window from scratch, which becomes prohibitive for large n (up to 10^6) and k (up to n). The optimal paradigm leverages incremental updates, reducing total work to O(n) while keeping auxiliary storage constant, making it ideal for real‑time streaming or high‑throughput data pipelines.

Interview Questions on This Problem

Q1How does the sliding window technique achieve O(n) time for fixed‑size window sums?

It updates the sum by subtracting the element that leaves the window and adding the new element that enters. This constant‑time adjustment eliminates the need to recompute the entire window.

Q2What edge cases must you handle when implementing sliding window sums?

When k equals 1, each element is its own sum, and when k equals the array length, there is only one window. Also, ensure the array length is at least k to avoid out‑of‑bounds errors.

Q3Can the sliding window be adapted for variable‑size windows, and what changes are required?

Yes, but you need a data structure (e.g., deque) to maintain aggregates as the window expands or contracts. The update logic then depends on the specific operation (min, max, sum, etc.).

Examples

Example 1

Input

5 3
1 2 3 4 5

Output

6 9 12

Explanation: The windows of size 3 are: - [1,2,3] → 1+2+3 = 6 - [2,3,4] → 2+3+4 = 9 - [3,4,5] → 3+4+5 = 12 Thus the output is 6 9 12.

Example 2

Input

4 2
10 -2 3 5

Output

8 1 8

Explanation: Windows: - [10,-2] → 10+(-2) = 8 - [-2,3] → -2+3 = 1 - [3,5] → 3+5 = 8 Output: 8 1 8.

Example 3

Input

4 1
0 0 0 0

Output

0 0 0 0

Explanation: With window size 1, each element is its own sum. All sums are 0.

Example 4

Input

4 3
-1 -2 -3 -4

Output

-6 -9

Explanation: Windows: - [-1,-2,-3] → -1-2-3 = -6 - [-2,-3,-4] → -2-3-4 = -9 Output: -6 -9.

Constraints

  • 1 <= n <= 100000
  • 1 <= k <= n
  • -1000000000 <= a_i <= 1000000000

Optimal Approach & Strategy

Use a sliding window: keep a running sum, subtract the element exiting, add the new element entering, achieving O(n) time.

Brute Force Approach

Compute each window's sum from scratch using a nested loop, leading to O(n·k) time.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function solution(nums, k) {
   let windowSum = 0;
   for (let i = 0; i < nums.length; i++) {
       if (i < k) {
           windowSum += nums[i];
       } else {
           windowSum = windowSum - nums[i - k] + nums[i];
       }
   }
   return windowSum;
}

Asked in Top Tech Interviews

GoogleAmazonMicrosoft

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.