v9.1.1
Menu

Sliding Window Maximum

hardPROHeaps & Priority QueuesDSA for AI Engineers

Given an array and sliding window of size k, return the maximum in each window position.

Output space-separated.

Example

Input
1,3,-1,-3,5,3,6,7
3
Output
3 3 5 5 6 7
Reasoning
  • The array is processed with a sliding window of size k=3k=3, starting from the first element.
  • For each window position, the maximum value is found:
    • Window 1: max⁡(1,3,−1)=3\max(1, 3, -1) = 3
    • Window 2: max⁡(3,−1,−3)=3\max(3, -1, -3) = 3
    • Window 3: max⁡(−1,−3,5)=5\max(-1, -3, 5) = 5
    • Window 4: max⁡(−3,5,3)=5\max(-3, 5, 3) = 5
    • Window 5: max⁡(5,3,6)=6\max(5, 3, 6) = 6
    • Window 6: max⁡(3,6,7)=7\max(3, 6, 7) = 7
  • The maximum values from each window are collected and output as space-separated values.
  • The final output is therefore: 3355673 3 5 5 6 7

Constraints

  • 1 <= len(nums) <= 10^5
  • -10^4 <= nums[i] <= 10^4
  • 1 <= k <= len(nums)

More from DSA for AI Engineers

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.