Find maximum of minimum for every window size in a given array
Last Updated :
02 Nov, 2022
Given an integer array arr[] of size N, find the maximum of the minimums for every window size in the given array.
Note: The window size varies from 1 to N.
Example:
Input: arr[] = {10, 20, 30, 50, 10, 70, 30}
Output: 70, 30, 20, 10, 10, 10, 10
Explanation:
The first element in the output indicates the maximum of minimums of all windows of size 1.
Minimums of windows of size 1 are {10}, {20}, {30}, {50}, {10}, {70} and {30}.
Maximum of these minimums is 70
The second element in the output indicates the maximum of minimums of all windows of size 2.
Minimums of windows of size 2 are {10}, {20}, {30}, {10}, {10}, and {30}.
Maximum of these minimums is 30
The third element in the output indicates the maximum of minimums of all windows of size 3.
Minimums of windows of size 3 are {10}, {20}, {10}, {10} and {10}.
Maximum of these minimums is 20
Similarly, other elements of output are computed.
Finding the Maximum of Minimums for every window size by Brute-force method:
The idea is to calculate the minimum of every window separately and print the maximum of each window size.
Follow the steps below to implement the above idea:
- Traverse a loop on K from1 till N
- Initialize a variable maxOfMin = INT_MIN
- Initialize a nested on i loop from 0 till N – K
- Initialize a variable min = arr[i]
- Initialize another nested loop on j from 1 till K
- If maxOfMin < min
- Print maxOfMin for the window of size K.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printMaxOfMin( int arr[], int n)
{
for ( int k = 1; k <= n; k++) {
int maxOfMin = INT_MIN;
for ( int i = 0; i <= n - k; i++) {
int min = arr[i];
for ( int j = 1; j < k; j++) {
if (arr[i + j] < min)
min = arr[i + j];
}
if (min > maxOfMin)
maxOfMin = min;
}
cout << maxOfMin << " " ;
}
}
int main()
{
int arr[] = { 10, 20, 30, 50, 10, 70, 30 };
int n = sizeof (arr) / sizeof (arr[0]);
printMaxOfMin(arr, n);
return 0;
}
|
Java
class Test {
static int arr[] = { 10 , 20 , 30 , 50 , 10 , 70 , 30 };
static void printMaxOfMin( int n)
{
for ( int k = 1 ; k <= n; k++) {
int maxOfMin = Integer.MIN_VALUE;
for ( int i = 0 ; i <= n - k; i++) {
int min = arr[i];
for ( int j = 1 ; j < k; j++) {
if (arr[i + j] < min)
min = arr[i + j];
}
if (min > maxOfMin)
maxOfMin = min;
}
System.out.print(maxOfMin + " " );
}
}
public static void main(String[] args)
{
printMaxOfMin(arr.length);
}
}
|
Python3
INT_MIN = - 1000000
def printMaxOfMin(arr, n):
for k in range ( 1 , n + 1 ):
maxOfMin = INT_MIN
for i in range (n - k + 1 ):
min = arr[i]
for j in range (k):
if (arr[i + j] < min ):
min = arr[i + j]
if ( min > maxOfMin):
maxOfMin = min
print (maxOfMin, end = " " )
arr = [ 10 , 20 , 30 , 50 , 10 , 70 , 30 ]
n = len (arr)
printMaxOfMin(arr, n)
|
C#
using System;
class GFG {
static int [] arr = { 10, 20, 30, 50, 10, 70, 30 };
static void printMaxOfMin( int n)
{
for ( int k = 1; k <= n; k++) {
int maxOfMin = int .MinValue;
for ( int i = 0; i <= n - k; i++) {
int min = arr[i];
for ( int j = 1; j < k; j++) {
if (arr[i + j] < min)
min = arr[i + j];
}
if (min > maxOfMin)
maxOfMin = min;
}
Console.Write(maxOfMin + " " );
}
}
public static void Main() { printMaxOfMin(arr.Length); }
}
|
PHP
<?php
function printMaxOfMin( $arr , $n )
{
for ( $k = 1; $k <= $n ; $k ++)
{
$maxOfMin = PHP_INT_MIN;
for ( $i = 0; $i <= $n - $k ; $i ++)
{
$min = $arr [ $i ];
for ( $j = 1; $j < $k ; $j ++)
{
if ( $arr [ $i + $j ] < $min )
$min = $arr [ $i + $j ];
}
if ( $min > $maxOfMin )
$maxOfMin = $min ;
}
echo $maxOfMin , " " ;
}
}
$arr = array (10, 20, 30, 50, 10, 70, 30);
$n = sizeof( $arr );
printMaxOfMin( $arr , $n );
?>
|
Javascript
<script>
var arr = [ 10, 20, 30, 50, 10, 70, 30 ];
function printMaxOfMin(n) {
for (k = 1; k <= n; k++) {
var maxOfMin = Number.MIN_VALUE;
for (i = 0; i <= n - k; i++) {
var min = arr[i];
for (j = 1; j < k; j++) {
if (arr[i + j] < min)
min = arr[i + j];
}
if (min > maxOfMin)
maxOfMin = min;
}
document.write(maxOfMin + " " );
}
}
printMaxOfMin(arr.length);
</script>
|
Output
70 30 20 10 10 10 10
Time Complexity: O(N3), where N is the size of the given array.
Auxiliary Space: O(1), constant extra space is being used.
Finding the Maximum of Minimums for every window size by using Stack:
The idea is to find the next smaller and previous smaller of each element and update the maximum of window with size as the difference in their indices.
Follow the steps below to implement the above idea:
- Initialize a stack s.
- Create two arrays, left and right of size N + 1 to store the next smaller and the previous smaller elements.
- Traverse a loop on i from 0 till N
- Assign left[i] = -1 and right[i] = N
- Traverse a loop on i from 0 till N
- Pop the elements from s while the current element is not greater than the element at top of stack s.
- If the stack is not empty
- Push current index in stack s
- Empty the stack s.
- Traverse a loop on i from N – 1 till 0
- Pop the elements from s while the current element is not greater than the element at top of stack s.
- If the stack is not empty
- Update right[i] = s.top()
- Push current index in stack s
- Initialize an array ans of size N + 1 with 0.
- Traverse a loop on i from 0 till N
- Initialize len = left[i] – right[i] + 1
- Update ans[len] = max(ans[len], arr[i])
- Traverse a loop on i from N – 1 till 0
- Update ans[i] = max(ans[i], ans[i + 1])
- Print ans array.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <stack>
using namespace std;
void printMaxOfMin( int arr[], int n)
{
stack< int > s;
int left[n + 1];
int right[n + 1];
for ( int i = 0; i < n; i++) {
left[i] = -1;
right[i] = n;
}
for ( int i = 0; i < n; i++) {
while (!s.empty() && arr[s.top()] >= arr[i])
s.pop();
if (!s.empty())
left[i] = s.top();
s.push(i);
}
while (!s.empty())
s.pop();
for ( int i = n - 1; i >= 0; i--) {
while (!s.empty() && arr[s.top()] >= arr[i])
s.pop();
if (!s.empty())
right[i] = s.top();
s.push(i);
}
int ans[n + 1];
for ( int i = 0; i <= n; i++)
ans[i] = 0;
for ( int i = 0; i < n; i++) {
int len = right[i] - left[i] - 1;
ans[len] = max(ans[len], arr[i]);
}
for ( int i = n - 1; i >= 1; i--)
ans[i] = max(ans[i], ans[i + 1]);
for ( int i = 1; i <= n; i++)
cout << ans[i] << " " ;
}
int main()
{
int arr[] = { 10, 20, 30, 50, 10, 70, 30 };
int n = sizeof (arr) / sizeof (arr[0]);
printMaxOfMin(arr, n);
return 0;
}
|
Java
import java.util.Stack;
class Test {
static int arr[] = { 10 , 20 , 30 , 50 , 10 , 70 , 30 };
static void printMaxOfMin( int n)
{
Stack<Integer> s = new Stack<>();
int left[] = new int [n + 1 ];
int right[] = new int [n + 1 ];
for ( int i = 0 ; i < n; i++) {
left[i] = - 1 ;
right[i] = n;
}
for ( int i = 0 ; i < n; i++) {
while (!s.empty() && arr[s.peek()] >= arr[i])
s.pop();
if (!s.empty())
left[i] = s.peek();
s.push(i);
}
while (!s.empty())
s.pop();
for ( int i = n - 1 ; i >= 0 ; i--) {
while (!s.empty() && arr[s.peek()] >= arr[i])
s.pop();
if (!s.empty())
right[i] = s.peek();
s.push(i);
}
int ans[] = new int [n + 1 ];
for ( int i = 0 ; i <= n; i++)
ans[i] = 0 ;
for ( int i = 0 ; i < n; i++) {
int len = right[i] - left[i] - 1 ;
ans[len] = Math.max(ans[len], arr[i]);
}
for ( int i = n - 1 ; i >= 1 ; i--)
ans[i] = Math.max(ans[i], ans[i + 1 ]);
for ( int i = 1 ; i <= n; i++)
System.out.print(ans[i] + " " );
}
public static void main(String[] args)
{
printMaxOfMin(arr.length);
}
}
|
Python3
def printMaxOfMin(arr, n):
s = []
left = [ - 1 ] * (n + 1 )
right = [n] * (n + 1 )
for i in range (n):
while ( len (s) ! = 0 and
arr[s[ - 1 ]] > = arr[i]):
s.pop()
if ( len (s) ! = 0 ):
left[i] = s[ - 1 ]
s.append(i)
while ( len (s) ! = 0 ):
s.pop()
for i in range (n - 1 , - 1 , - 1 ):
while ( len (s) ! = 0 and arr[s[ - 1 ]] > = arr[i]):
s.pop()
if ( len (s) ! = 0 ):
right[i] = s[ - 1 ]
s.append(i)
ans = [ 0 ] * (n + 1 )
for i in range (n + 1 ):
ans[i] = 0
for i in range (n):
Len = right[i] - left[i] - 1
ans[ Len ] = max (ans[ Len ], arr[i])
for i in range (n - 1 , 0 , - 1 ):
ans[i] = max (ans[i], ans[i + 1 ])
for i in range ( 1 , n + 1 ):
print (ans[i], end = " " )
if __name__ = = '__main__' :
arr = [ 10 , 20 , 30 , 50 , 10 , 70 , 30 ]
n = len (arr)
printMaxOfMin(arr, n)
|
C#
using System;
using System.Collections.Generic;
class GFG {
public static int [] arr
= new int [] { 10, 20, 30, 50, 10, 70, 30 };
public static void printMaxOfMin( int n)
{
Stack< int > s = new Stack< int >();
int [] left = new int [n + 1];
int [] right = new int [n + 1];
for ( int i = 0; i < n; i++) {
left[i] = -1;
right[i] = n;
}
for ( int i = 0; i < n; i++) {
while (s.Count > 0 && arr[s.Peek()] >= arr[i]) {
s.Pop();
}
if (s.Count > 0) {
left[i] = s.Peek();
}
s.Push(i);
}
while (s.Count > 0) {
s.Pop();
}
for ( int i = n - 1; i >= 0; i--) {
while (s.Count > 0 && arr[s.Peek()] >= arr[i]) {
s.Pop();
}
if (s.Count > 0) {
right[i] = s.Peek();
}
s.Push(i);
}
int [] ans = new int [n + 1];
for ( int i = 0; i <= n; i++) {
ans[i] = 0;
}
for ( int i = 0; i < n; i++) {
int len = right[i] - left[i] - 1;
ans[len] = Math.Max(ans[len], arr[i]);
}
for ( int i = n - 1; i >= 1; i--) {
ans[i] = Math.Max(ans[i], ans[i + 1]);
}
for ( int i = 1; i <= n; i++) {
Console.Write(ans[i] + " " );
}
}
public static void Main( string [] args)
{
printMaxOfMin(arr.Length);
}
}
|
Javascript
<script>
let arr = [10, 20, 30, 50, 10, 70, 30];
function printMaxOfMin(n)
{
let s = [];
let left = new Array(n + 1);
left.fill(0);
let right = new Array(n + 1);
right.fill(0);
for (let i = 0; i < n; i++)
{
left[i] = -1;
right[i] = n;
}
for (let i = 0; i < n; i++)
{
while (s.length > 0 && arr[s[s.length - 1]] >= arr[i])
{
s.pop();
}
if (s.length > 0)
{
left[i] = s[s.length - 1];
}
s.push(i);
}
while (s.length > 0)
{
s.pop();
}
for (let i = n - 1 ; i >= 0 ; i--)
{
while (s.length > 0 && arr[s[s.length - 1]] >= arr[i])
{
s.pop();
}
if (s.length > 0)
{
right[i] = s[s.length - 1];
}
s.push(i);
}
let ans = new Array(n + 1);
ans.fill(0);
for (let i = 0; i <= n; i++)
{
ans[i] = 0;
}
for (let i = 0; i < n; i++)
{
let len = right[i] - left[i] - 1;
ans[len] = Math.max(ans[len], arr[i]);
}
for (let i = n - 1; i >= 1; i--)
{
ans[i] = Math.max(ans[i], ans[i + 1]);
}
for (let i = 1; i <= n; i++)
{
document.write(ans[i] + " " );
}
}
printMaxOfMin(arr.length);
</script>
|
Output
70 30 20 10 10 10 10
Time Complexity: O(N), where N is the size of the given array.
Auxiliary Space: O(N), for using stack and additional arrays.
Ekta Goel and Ayush Govil
Please Login to comment...