VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-the-maximum-of-minimums-for-every-window-size-in-a-given-array/

⇱ Max of Mins for All Window Sizes - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Max of Mins for All Window Sizes

Last Updated : 14 Mar, 2026

Given an integer array arr[], find the maximum of minimums for every window size from 1 to n. For each window size k, consider all contiguous subarrays of length k and determine the minimum element in each subarray. Among these minimum values, take the maximum one.

Example:

Input: arr[] = [10, 20, 30]
Output: [30, 20, 10]
Explanation: The max of min for every possible window size.
Size 1: min of [10], [20], [30], max of min is 30.
Size 2: min of [10, 20],[20,30] max of min is 20.
Size 3: min of [10,20,30], max of min is 10.

Input: arr[] = [10, 20, 30, 50, 10, 70, 30]
Output: [70, 30, 20, 10, 10, 10, 10]
Explanation:
Size 1: min are [10, 20, 30, 50, 10, 70, 30], max of min is 70.
Size 2: min are [10, 20, 30, 10, 10, 30], max of min is 30.
Size 3: min are [10, 20, 10, 10, 10], max of min is 20.
Size 4–7: min are [10, 10, 10, 10], max of min is 10.

[Naive Approach] By Checking All Subarrays O(n^3) Time and O(1) Space

Compute the minimum of every possible window, For each window size from 1 to n and find the maximum of those minimums.


Output
70 30 20 10 10 10 10 

[Better Approach] Sliding Window Minimum using Deque - O(n^2) Time and O(n) Space

Use a deque with the sliding window technique to find the minimum element of every subarray of size k efficiently. Repeat this for k = 1 to n, and for each k, take the maximum among these minimum values.


Output
70 30 20 10 10 10 10 

[Expected Approach] Using Stack - O(n) Time and O(n) Space

  • For each element, find the previous smaller and next smaller using a stack.
  • These boundaries give the largest window size where the current element is the minimum.
  • Update the answer for that window size with the maximum value of such minimums.
  • Finally, propagate results backward so smaller windows have at least as large values as larger windows.

Output
70 30 20 10 10 10 10 
Comment
Article Tags: