Search⌘ K
AI Features

Sliding Window Maximum

Explore how to efficiently solve the sliding window maximum problem by finding the maximum value within each window as it moves across an integer array. This lesson teaches you the sliding window technique to handle subarray challenges, helping you optimize and implement solutions step-by-step.

Statement

You are given an array of integers nums and a sliding window of size w that moves from left to right across the array, shifting one position at a time.

Your task is to find the maximum value within the current window at each step and return it.

Constraints:

  • 11 \leq nums.length 103\leq 10^3

  • 104-10^4 \leq nums[i] 104\leq 10^4

  • 11 \leq w \leq nums.length

Examples

Understand the problem

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps us to check if you’re solving the correct problem:

Find Maximum in Sliding Window

1.

What should be the output if the following input is given?

nums = [-4, 5, 4, -4, 4, 6, 7, 20]

w = 2

A.

[5, 4, 6, 20]

B.

[5, 20]

C.

[5, 5, 4, 4, 6, 7, 20]

D.

[5, 5, 4, 6, 6]


1 / 4

Figure it out!

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4
5
6

Try it yourself

Implement your solution in the following coding playground:

Java
usercode > SlidingWindowMaximum.java
import java.util.*;
class SlidingWindowMaximum {
public static int[] findMaxSlidingWindow(int[] nums, int w) {
// Replace this placeholder return statement with your code
return new int[0];
}
}
Sliding Window Maximum