Find maximum of minimum for every window size in a given array in C++


In this problem, we are given an array arr[] of size n. Our task is to Find the maximum of the minimum for every window size in a given array.

Problem Description − We need to find the maximum of the minimum of a window size that varies from 1 to n. For this we will consider subarrays of the given window size, find the minimum element of each subarray and then find the maximum of all the minimums.

Let’s take an example to understand the problem,

Input

arr[] = {4, 1, 2, 4, 5, 1, 2, 4}

Output

5 4 2 1 1 1 1 1

Explanation

Window Size :
1 => windows { (4), (1), (2), (4), (5), (1), (2), (4) } => minimum = {4, 1, 2, 4, 5, 1, 2, 4} => maximum of minimums = 5
2 => windows { (4, 1), (1, 2), (2, 4), (4, 5), (5, 1), (1, 2), (2, 4) } => minimum = {1, 1, 2, 4, 1, 1, 2} => maximum of minimums = 4
3 => windows { (4, 1, 2), (1, 2, 4), (2, 4, 5), (4, 5, 1), (5, 1, 2), (1, 2, 4) } => minimum = {1, 1, 2, 1, 1, 1} => maximum of minimums = 2
4 => windows { (4, 1, 2, 4), (1, 2, 4, 5), (2, 4, 5, 1), (4, 5, 1, 2), (5, 1, 2, 4) }=> minimum = {1, 1, 1, 1, 1} => maximum of minimums = 1
5 => windows { (4, 1, 2, 4, 5), (1, 2, 4, 5, 1), (2, 4, 5, 1, 2), (4, 5, 1, 2, 4) } => minimum = {1, 1, 1, 1} => maximum of minimums = 1
6 => windows { (4, 1, 2, 4, 5, 1), (1, 2, 4, 5, 1, 2), (2, 4, 5, 1, 2, 4) } => minimum = {1, 1, 1} => maximum of minimums = 1
7 => windows { (4, 1, 2, 4, 5, 1, 2), (1, 2, 4, 5, 1, 2, 4) } => minimum = {1, 1} => maximum of minimums = 1
7 => windows { (4, 1, 2, 4, 5, 1, 2, 4) } => minimum = {1} => maximum of
minimums = 1

Solution Approach

A simple solution to the problem is by creating all windows of size 1 to n. Then for each window of the given size, we will be finding all subarrays of given size. For the array we will find the minimum of each subarray and then return the maximum of all minimums.

At the end of each window size iteration, we will print all maximums of minimums in scala

Program to illustrate the working of our solution,

Example

 Live Demo

#include <iostream>
using namespace std;
void printMaxMinWindowK(int arr[], int n, int k) {
   int maxMin = -1;
   int minEle = -1;
   for (int i = 0; i <= n-k; i++) {
      minEle = arr[i];
      for (int j = 1; j < k; j++) {
         if (arr[i+j] < minEle)
            minEle = arr[i+j];
      }
      if (minEle > maxMin)
         maxMin = minEle;
   }
   cout<<maxMin<<endl;
}
int main() {
   int arr[] = {4, 1, 2, 4, 5, 1, 2, 4};
   int n = sizeof(arr)/sizeof(arr[0]);
   for(int i = 1; i < n; i++){
      cout<<"Window Size :"<<i<<", maximum of minimum : ";
      printMaxMinWindowK(arr, n, i);
   }
   return 0;
}

Output

Window Size :1, maximum of minimum : 70
Window Size :2, maximum of minimum : 30
Window Size :3, maximum of minimum : 20
Window Size :4, maximum of minimum : 10
Window Size :5, maximum of minimum : 10
Window Size :6, maximum of minimum : 10

Alternate Solution

A simple solution to the problem is using extra memory space, creating an auxiliary array. We will use an array to store the next smallest element for the current element. And another to store the previous smallest element. Using these arrays we will find the element for array element of index i. The element arr[i] is the minimum of the window of length “right[i] - left[i] + 1”. Using this, we will find the maximum of minimums for the given window.

Program to illustrate the working of our solution,

Example

 Live Demo

#include <iostream>
#include<stack>
using namespace std;
void printMaxMinWindow(int arr[], int n) {
   stack<int> s;
   int prev[n+1];
   int next[n+1];
   for (int i=0; i<n; i++) {
      prev[i] = -1;
      next[i] = n;
   }
   for (int i=0; i<n; i++) {
      while (!s.empty() && arr[s.top()] >= arr[i])
         s.pop();
         if (!s.empty())
            prev[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())
               next[i] = s.top();
               s.push(i);
      }
      int maxOfMin[n+1];
      for (int i=0; i<=n; i++)
         maxOfMin[i] = 0;
         for (int i=0; i<n; i++) {
            int len = next[i] - prev[i] - 1;
            maxOfMin[len] = max(maxOfMin[len], arr[i]);
         }
         for (int i=n-1; i>=1; i--)
            maxOfMin[i] = max(maxOfMin[i], maxOfMin[i+1]);
         for (int i=1; i<=n; i++)
            cout<<"Window Size: "<<i<<", maximum of minimum : "<<maxOfMin[i]<<endl;
}
int main() {
   int arr[] = {4, 1, 2, 4, 5, 1, 2, 4};
   int n = sizeof(arr)/sizeof(arr[0]);
   printMaxMinWindow(arr, n);
   return 0;
}

Output

Window Size: 1, maximum of minimum : 5
Window Size: 2, maximum of minimum : 4
Window Size: 3, maximum of minimum : 2
Window Size: 4, maximum of minimum : 1
Window Size: 5, maximum of minimum : 1
Window Size: 6, maximum of minimum : 1
Window Size: 7, maximum of minimum : 1
Window Size: 8, maximum of minimum : 1

Updated on: 12-Mar-2021

591 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements