Java Array Program · A40
Sliding Window Maximum in Java
Keep a deque of indexes whose values decrease from front to back. The front is always the window's maximum; smaller values behind a new larger value can be dropped forever.
Problem Statement
Given an array and a window size k, return the maximum value of every contiguous window of length k, from left to right.
Assume 1 ≤ k ≤ n, so there are n - k + 1 windows.
Input/Output Examples
[4, 2, 12, 3, 8, 1, 7], k = 3Output[12, 12, 12, 8, 8]Explanation: 12 dominates the first three windows.[4, 2, 12], k = 1Output[4, 2, 12]Explanation: each window is a single element.[9, 8, 7], k = 2Output[9, 8]Explanation: the first element of each window is its maximum.Visual Explanation
- i = 2deque1212 removes 2 and 4 from the back max 12
- i = 3, 4deque1283 is removed by 8 max 12
- i = 5deque8112 leaves the window max 8
- i = 6deque877 removes 1 max 8
- Done[12, 12, 12, 8, 8]
[4, 2, 12, 3, 8, 1, 7] with k = 3: new values remove smaller ones from the back, and indexes that leave the window are removed from the front.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.
Approach 1: Monotonic Deque (Recommended)
Idea: store indexes with decreasing values; pop from the front when they leave the window and from the back when a larger value arrives.
- Remove the front index if it is outside the window (
≤ i - k). - Pop indexes from the back while their values are
≤the new value. - Add the new index at the back.
- Once
i ≥ k - 1, record the value at the front.
Time: O(n) · Space: O(k).
Approach 2: Scan Every Window
Idea: for each window, find its maximum with a loop.
- For each start position, scan the next
kelements. - Record the largest value.
Time: O(n × k) · Space: O(1).
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.
Approach 1: Monotonic Deque (Recommended)
Storing indexes, not values, is what lets the front be checked against the window boundary.
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
public class WindowMaxDeque {
// The deque holds indexes with decreasing values; its front is the current window's maximum
static int[] maxSlidingWindow(int[] arr, int k) {
int[] result = new int[arr.length - k + 1];
Deque<Integer> dq = new ArrayDeque<>();
for (int i = 0; i < arr.length; i++) {
if (!dq.isEmpty() && dq.peekFirst() <= i - k) {
dq.pollFirst(); // index left the window
}
while (!dq.isEmpty() && arr[dq.peekLast()] <= arr[i]) {
dq.pollLast(); // smaller values can never be a maximum again
}
dq.addLast(i);
if (i >= k - 1) {
result[i - k + 1] = arr[dq.peekFirst()];
}
}
return result;
}
public static void main(String[] args) {
Object[][] cases = {{new int[] {4, 2, 12, 3, 8, 1, 7}, 3}, {new int[] {4, 2, 12}, 1}, {new int[] {9, 8, 7}, 2}};
for (Object[] c : cases) {
System.out.println(Arrays.toString((int[]) c[0]) + ", k=" + c[1] + " -> "
+ Arrays.toString(maxSlidingWindow((int[]) c[0], (Integer) c[1])));
}
}
}
[4, 2, 12, 3, 8, 1, 7], k=3 -> [12, 12, 12, 8, 8] [4, 2, 12], k=1 -> [4, 2, 12] [9, 8, 7], k=2 -> [9, 8]
Time complexity: O(n) · Space complexity: O(k).
Approach 2: Scan Every Window
Fine for small k, and a clear reference.
import java.util.Arrays;
public class WindowMaxBrute {
// Scans every window separately: O(n * k)
static int[] maxSlidingWindow(int[] arr, int k) {
int[] result = new int[arr.length - k + 1];
for (int start = 0; start + k <= arr.length; start++) {
int max = arr[start];
for (int i = start + 1; i < start + k; i++) {
max = Math.max(max, arr[i]);
}
result[start] = max;
}
return result;
}
public static void main(String[] args) {
Object[][] cases = {{new int[] {4, 2, 12, 3, 8, 1, 7}, 3}, {new int[] {4, 2, 12}, 1}, {new int[] {9, 8, 7}, 2}};
for (Object[] c : cases) {
System.out.println(Arrays.toString((int[]) c[0]) + ", k=" + c[1] + " -> "
+ Arrays.toString(maxSlidingWindow((int[]) c[0], (Integer) c[1])));
}
}
}
[4, 2, 12, 3, 8, 1, 7], k=3 -> [12, 12, 12, 8, 8] [4, 2, 12], k=1 -> [4, 2, 12] [9, 8, 7], k=2 -> [9, 8]
Time complexity: O(n × k) · Space complexity: O(1).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Monotonic Deque | O(n) | O(k) |
| Scan Every Window | O(n × k) | O(1) |
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.