Maximum of minimum for every window size

Hard
0/120
Average time to solve is 45m
210 upvotes
Asked in companies
MicrosoftCognizantIBM

Problem statement

You are given an array of ‘N’ integers, you need to find the maximum of minimum for every window size. The size of the window should vary from 1 to ‘N’ only.

For example:

ARR = [1,2,3,4]
Minimums of window size 1 = min(1), min(2), min(3), min(4) = 1,2,3,4
Maximum among (1,2,3,4)  is 4

Minimums of window size 2 = min(1,2), min(2,3),   min(3,4) = 1,2,3
Maximum among (1,2,3) is 3

Minimums of window size 3 = min(1,2,3), min(2,3,4) = 1,2
Maximum among (1,2) is 2

Minimums of window size 4 = min(1,2,3,4) = 1
Maximum among them is 1
The output array should be [4,3,2,1].
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of the input contains an integer ‘T’ denoting the number of test cases.

The first line of each test case contains a single positive integer ‘N’ denoting the number of the elements present in the array.

The second line of each test case contains ‘N’ space-separated integers denoting the elements of the array.
Output Format:
The only line of output of each test case should contain ‘N’ space-separated integer such that he ith integer indicates the maximum of minimums of the windows of size ‘i’.
Constraints:
1 <= T <= 100
1 <= N <= 10 ^ 4 
-10 ^ 9 <= ARR[i] <= 10 ^ 9

Where ‘T’ is the number of test cases, ‘N’ is the size of the array and ‘ARR[i]’ is the size of the array elements.

Time Limit: 1 sec
Sample Input 1:
2
4
1 2 3 4
5
3 3 4 2 4    
Sample Output 1:
4 3 2 1
4 3 3 2 2
Explanation for sample input 1:
Test case 1:
Already explained in the question.

Test case 2:
Minimums of window size 1 = min(3), min(3), min(4), min(2), min(4) = 3, 3, 4, 2, 4
Maximum among (3, 3, 4, 2, 4) is 4

Minimums of window size 2 = min(3,3), min(3,4), min(4,2), min(2,4) = 3, 3, 2, 2
Maximum among (3, 3, 2, 2) is 3

Minimums of window size 3 = min(3,3,4), min(3,4,2), min(4,2,4) = 3, 2, 2
Maximum among (3, 2, 2) is 3

Minimums of window size 4 = min(3,3,4,2), min(3,4,2,4) = 2, 2
Maximum among (2, 2) is 2

Minimums of window size 4 = min(3,3,4,2,4) = 2
Maximum among them is 2
The output array should be [4,3,3,2,2].
Sample Input 2:
2
5 
45 -2 42 5 -11 
6 
-2 12 -1 1 20 1 
Sample Output 2:
 45 5 -2 -2 -11
 20 1  1 -1 -1 -2
Hint

Find the minimum value for every possible window and choose the maximum among them.

Approaches (2)
Brute Force
  • We will use two nested loops for sliding on the window of every possible size and one more inner loop to traverse on the window and store the minimum element of the current window in a ‘temp’ variable.
  • We will create an array named ‘answer’. The ‘answer[i]’ will store the maximum of all the available minimum of every window size ‘i’.
  • If ‘i’ and ‘j’  are the starting and ending indexes of the window then its length = j-i+1.
  • So we update our ‘answer[length]’ with the maximum of all the available minimum of every window size ‘i’ with the help of a ‘temp’ variable
    i.e, ‘answer[length]’ = max( answer[length] , temp ).
Time Complexity

O(N ^ 3), where ‘N’ is the number of elements present in the array.

 

As we ran 3 nested loops in total, 2 for sliding over the window and one for calculating over the answer for the current window. Hence, the overall Time Complexity is O(N ^ 3).

Space Complexity

O(1). 
 

Constant space is being used. Hence, the overall Space Complexity is O(1).

Video Solution
Unlock at level 3
(75% EXP penalty)
Code Solution
(100% EXP penalty)
Maximum of minimum for every window size
Full screen
Console