GeeksforGeeks Hard

Maximum of Minimums for Every Window Size

Maximum of Minimums for Every Window Size is a GFG problem (Hard). For every window size k from 1 to n, take the minimum of each contiguous subarray of length k, then report the largest of those minimums. Return the n answers, the one for size 1 first.

  • With n up to 10⁵, solving each size on its own costs at least O(n²) in total, which is too slow.
  • All values are positive, so 0 works as an empty answer slot.
Constraints
  • 1 <= arr.size() <= 10⁵
  • 1 <= arr[i] <= 10⁶
stackmonotonic-stacksliding-window
Open on GeeksforGeeks ↗
02

Intuition

Instead of asking every window for its minimum, ask every element: what is the widest window in which I am the minimum?

  • Its reach: arr[j] is the minimum of any window that stays strictly between the nearest smaller value on its left and the nearest smaller value on its right. If those sit at left and right, the widest such window has length right − left − 1.
  • Smaller sizes inherit it: any window of size w + 1 contains a window of size w whose minimum is at least as large, so the answers never increase as the size grows.

So record each element at its widest size, then carry the best values down to smaller sizes.

How to spot this pattern

"For each element, how far does it stay the minimum (or maximum)?" is the nearest-smaller-element pattern solved with a monotonic stack. Largest Rectangle in Histogram and Sum of Subarray Minimums use the same bounds.

03

Approach

Try it first

Before reading on, pick one element and work out the widest window in which it is the minimum. Which two neighbours decide that width? Then decide how one element's answer helps window sizes smaller than its widest one. Aim for O(n).

1

Keep a stack of increasing values

Scan i from 0 to n and keep a stack of indices whose values rise from bottom to top. Before pushing i, pop every index whose value is >= arr[i], because those elements can no longer be the minimum of a window that reaches i.

2

Read both bounds when an index is popped

When index j is popped, arr[i] is the first value to its right that is not larger, and the index now on top of the stack is the nearest smaller value to its left (or -1 if the stack is empty). One pop gives both bounds, so no second pass is needed.

3

Record the element at its widest size

The window between those bounds has size = i − left − 1, and arr[j] is its minimum. Set res[size] = max(res[size], arr[j]). Treating i == n as smaller than everything pops the indices left at the end, with the array's end as their right bound.

4

Fill the sizes nobody recorded

Some sizes get no element directly. Sweep size from n − 1 down to 1 and set res[size] = max(res[size], res[size + 1]). This is safe because the answer for a smaller window is never below the answer for a larger one.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def maxOfMins(self, arr):
▶3 n = len(arr)
▶4 res = [0] * (n + 1)
▶5 stack = []
▶6 for i in range(n + 1):
▶7 while stack and (i == n or arr[stack[-1]] >= arr[i]):
▶8 j = stack.pop()
▶9 left = stack[-1] if stack else -1
▶10 size = i - left - 1
▶11 res[size] = max(res[size], arr[j])
▶12 stack.append(i)
▶13 for size in range(n - 1, 0, -1):
▶14 res[size] = max(res[size], res[size + 1])
▶15 return res[1:]
100201302503104705306stack10at 0bestsize 1size 2size 3size 4size 5size 6size 7push 10 onto the empty stack
i0
stack10values, bottom to top
Index 0 goes on the stack. Its right bound is not known yet: it waits until a value not larger than 10 arrives.
100201302503104705306stack10at 020at 1bestsize 1size 2size 3size 4size 5size 6size 7push 20, larger than 10 below it
i1
stack10, 20values, bottom to top
Index 1 goes on the stack. Its right bound is not known yet: it waits until a value not larger than 20 arrives. The value below it, 10, is smaller, so the stack still rises from bottom to top.
100201302503104705306stack10at 020at 130at 2bestsize 1size 2size 3size 4size 5size 6size 7push 30, larger than 20 below it
i2
stack10, 20, 30values, bottom to top
Index 2 goes on the stack. Its right bound is not known yet: it waits until a value not larger than 30 arrives. The value below it, 20, is smaller, so the stack still rises from bottom to top.
100201302503104705306stack10at 020at 130at 250at 3bestsize 1size 2size 3size 4size 5size 6size 7push 50, larger than 30 below it
i3
stack10, 20, 30, 50values, bottom to top
Index 3 goes on the stack. Its right bound is not known yet: it waits until a value not larger than 50 arrives. The value below it, 30, is smaller, so the stack still rises from bottom to top.
100201302503104705306size 1stack10at 020at 130at 2best50size 1size 2size 3size 4size 5size 6size 710 arrives → pop 50, minimum of a size-1 window
popped50index 3
boundsindex 2 and index 4
size1
10 is smaller than 50, so 50 cannot be the minimum of any window that reaches index 4. Its right bound is index 4, and its left bound is index 2, the value just below it on the stack, 30. So 50 is the minimum of indexes 3 to 3, a window of size 1. It is the first value in that slot.
100201302503104705306size 2stack10at 020at 1best50size 130size 2size 3size 4size 5size 6size 710 arrives → pop 30, minimum of a size-2 window
popped30index 2
boundsindex 1 and index 4
size2
10 is smaller than 30, so 30 cannot be the minimum of any window that reaches index 4. Its right bound is index 4, and its left bound is index 1, the value just below it on the stack, 20. So 30 is the minimum of indexes 2 to 3, a window of size 2. It is the first value in that slot.
100201302503104705306size 3stack10at 0best50size 130size 220size 3size 4size 5size 6size 710 arrives → pop 20, minimum of a size-3 window
popped20index 1
boundsindex 0 and index 4
size3
10 is smaller than 20, so 20 cannot be the minimum of any window that reaches index 4. Its right bound is index 4, and its left bound is index 0, the value just below it on the stack, 10. So 20 is the minimum of indexes 1 to 3, a window of size 3. It is the first value in that slot.
100201302503104705306size 4stackemptybest50size 130size 220size 310size 4size 5size 6size 710 arrives → pop 10, minimum of a size-4 window
popped10index 0
boundsstart and index 4
size4
The new 10 at index 4 equals it. Any window that reaches index 4 has the same minimum, and the newer 10 will record that wider window when it is popped, so this one can stop here. Its right bound is index 4, and its left bound is the start, since the stack below it is empty. So 10 is the minimum of indexes 0 to 3, a window of size 4. It is the first value in that slot.
100201302503104705306stack10at 4best50size 130size 220size 310size 4size 5size 6size 7push 10 onto the empty stack
i4
stack10values, bottom to top
Index 4 goes on the stack. Its right bound is not known yet: it waits until a value not larger than 10 arrives.
100201302503104705306stack10at 470at 5best50size 130size 220size 310size 4size 5size 6size 7push 70, larger than 10 below it
i5
stack10, 70values, bottom to top
Index 5 goes on the stack. Its right bound is not known yet: it waits until a value not larger than 70 arrives. The value below it, 10, is smaller, so the stack still rises from bottom to top.
100201302503104705306size 1stack10at 4best70size 130size 220size 310size 4size 5size 6size 730 arrives → pop 70, minimum of a size-1 window
popped70index 5
boundsindex 4 and index 6
size1
30 is smaller than 70, so 70 cannot be the minimum of any window that reaches index 6. Its right bound is index 6, and its left bound is index 4, the value just below it on the stack, 10. So 70 is the minimum of indexes 5 to 5, a window of size 1. It beats 50 in that slot.
100201302503104705306stack10at 430at 6best70size 130size 220size 310size 4size 5size 6size 7push 30, larger than 10 below it
i6
stack10, 30values, bottom to top
Index 6 goes on the stack. Its right bound is not known yet: it waits until a value not larger than 30 arrives. The value below it, 10, is smaller, so the stack still rises from bottom to top.
100201302503104705306size 2stack10at 4best70size 130size 220size 310size 4size 5size 6size 7end of array → pop 30, minimum of a size-2 window
popped30index 6
boundsindex 4 and end
size2
The scan has passed the last element, so everything left on the stack is popped. Its right bound is the end of the array, and its left bound is index 4, the value just below it on the stack, 10. So 30 is the minimum of indexes 5 to 6, a window of size 2. That slot already holds 30, which is larger.
100201302503104705306size 7stackemptybest70size 130size 220size 310size 4size 5size 610size 7end of array → pop 10, minimum of a size-7 window
popped10index 4
boundsstart and end
size7
The scan has passed the last element, so everything left on the stack is popped. Its right bound is the end of the array, and its left bound is the start, since the stack below it is empty. So 10 is the minimum of indexes 0 to 6, a window of size 7. It is the first value in that slot.
100201302503104705306stackemptybest70size 130size 220size 310size 4size 510size 610size 7size 6 takes 10 from size 7
size6
best10
No element had a widest window of exactly size 6, but every window of size 7 contains a window of size 6 with a minimum at least as large. So size 6 can do at least 10.
100201302503104705306stackemptybest70size 130size 220size 310size 410size 510size 610size 7size 5 takes 10 from size 6
size5
best10
No element had a widest window of exactly size 5, but every window of size 6 contains a window of size 5 with a minimum at least as large. So size 5 can do at least 10.
100201302503104705306stackemptybest70size 130size 220size 310size 410size 510size 610size 7size 4 keeps 10
size4
best10
The value recorded for size 4 is already at least the answer for size 5, so it stays.
100201302503104705306stackemptybest70size 130size 220size 310size 410size 510size 610size 7size 3 keeps 20, above 10
size3
best20
The value recorded for size 3 is already at least the answer for size 4, so it stays.
100201302503104705306stackemptybest70size 130size 220size 310size 410size 510size 610size 7size 2 keeps 30, above 20
size2
best30
The value recorded for size 2 is already at least the answer for size 3, so it stays.
100201302503104705306stackemptybest70size 130size 220size 310size 410size 510size 610size 7size 1 keeps 70, above 30
size1
best70
The value recorded for size 1 is already at least the answer for size 2, so it stays.
100201302503104705306stackemptybest70size 130size 220size 310size 410size 510size 610size 7answer: 70, 30, 20, 10, 10, 10, 10
return[70, 30, 20, 10, 10, 10, 10]
Every size now holds its answer. The list never rises from left to right, the first entry is the largest element and the last is the smallest, as it must be.
05

Common pitfalls

Skipping the backward fill

✗ Wrong
return res[1:]  # right after the stack loop
✓ Right
for size in range(n - 1, 0, -1):
    res[size] = max(res[size], res[size + 1])
return res[1:]

No element is the minimum of a widest window of every size. For [10, 20, 30, 50, 10, 70, 30] nothing lands on sizes 5 and 6, so the result reads [70, 30, 20, 10, 0, 0, 10] instead of [70, 30, 20, 10, 10, 10, 10].

Stopping at equal values on both sides

✗ Wrong
# two-pass version, both passes
while stack and arr[stack[-1]] > arr[i]:
✓ Right
# at least one pass pops equal values
while stack and arr[stack[-1]] >= arr[i]:

Many solutions find the left and right bounds in two separate passes. If both passes treat an equal value as a wall, a run of equal values is cut into pieces and its full width is never recorded: [1, 1] returns [1, 0]. The one-pass code above is safe either way, because the last equal value popped still sees the full width.

06

Edge cases

A single element

It is popped when i == n with the empty stack as its left bound, so its size is 1 and the answer is that element.

All values equal

For [5, 5, 5] each push pops the previous equal value. The last one is popped at the end with size 3, and the fill copies 5 to every size.

07

Complexity

Time
O(n)
Space
O(n)
Each index is pushed once and popped once, and the fill pass is one loop over the sizes. The stack and the answer array each hold up to n + 1 entries.
08

Maximum of Minimums for Every Window Size FAQ

Why not run a sliding window minimum for every size?

A deque finds all minimums for one window size in O(n), so all n sizes take O(n²) time. At n = 10⁵ that is about 10¹⁰ steps, far too slow. The stack handles every size in one pass.

LeetCode 1950 looks the same. Is it?

Yes. Maximum of Minimum Values in All Subarrays (LeetCode 1950, premium) asks for the same n answers, using query index i for window size i + 1. The same code works unchanged.

Does the answer for size 1 always equal the largest element?

Yes, and the answer for size n is always the smallest element. A window of size 1 is a single element, so its minimum is the element itself; the only window of size n is the whole array. Both make quick checks for your output.