BackhardSliding Window

Maximum Values in Subsequences Solution

Problem Statement

Given an array of integers and a window size, find the maximum value in each window of the given size as it slides through the array from left to right.

Example 1
Input
arr = [4, 2, 9, 7, 5, 3, 1], windowSize = 3
Output
[9,9,9,7,5]

Explanation: The windows are [4, 2, 9], [2, 9, 7], [9, 7, 5], [7, 5, 3], [5, 3, 1] and their respective maxima are 9, 9, 7, 5, 3.

Example 2
Input
arr = [1, 3, 5, 2, 6, 4], windowSize = 2
Output
[3, 5, 5, 6, 6]

Explanation: The windows are [1, 3], [3, 5], [5, 2], [2, 6], [6, 4] and their respective maxima are 3, 5, 5, 6, 6.

Constraints

  • 1 <= windowSize <= arr.length
  • arr.length will not exceed 10^5
  • -10^9 <= arr[i] <= 10^9
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free