00:00

Problem Statement

Input/Output Examples

Visual Explanation

A value smaller than a newer value can never be a window maximum again, because the newer value stays in the window at least as long. Each index enters and leaves the deque at most once, so the total work is linear.

Approaches

The deque gives a linear-time answer. The brute force scans each window separately.

Java Solutions

Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.

Complexity Analysis

Here n is the array length and k the window size. Each index is added and removed from the deque at most once; the brute force re-scans k elements per window.

Interview Tips and Recommended Approach

  • Store indexes. You need them to know when the front leaves the window.
  • Explain why smaller values can be dropped. A newer, larger value outlives them.
  • Use ArrayDeque. It supports fast operations at both ends.
  • Know the related problems: sliding window minimum and the next greater element use the same monotonic idea.

Recommended Approach: Monotonic Deque

Why: it produces every window maximum in linear time, and the monotonic deque is a reusable pattern for many window problems.