JavaScript Program to Find Maximum of Minimum for Every Window Size in a Given Array
Last Updated :
15 May, 2024
Given an array of integers, find the maximum of minimums for every window size in the array. The window size varies from 1 to the size of the array.
Example:
Input: Array: [1, 2, 3, 5, 1, 7, 3]
Output: [7, 3, 2, 1, 1, 1, 1]
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 {1}, {2}, {3}, {5}, {1}, {7} and {3}.
Maximum of these minimums is 7
The second element in the output indicates the maximum of minimums of all windows of size 2.
Minimums of windows of size 2 are {1}, {2}, {3}, {1}, {1}, and {3}.
Maximum of these minimums is 3
The third element in the output indicates the maximum of minimums of all windows of size 3.
Minimums of windows of size 3 are {1}, {2}, {1}, {1} and {1}.
Maximum of these minimums is 2
Similarly, other elements of output are computed.
Below are the approaches to find maximum of minimum for every window size in a given array:
Using Iteration
The approach is to iterates through all possible window sizes from 1 to the length of the input array. Also, for each window size, we need to iterates through all starting indices of the array to calculate the minimum value within that window. Then, we can update the maximum of those minimum values seen so far for the current window size.
Example: This JavaScript code is the implementation of above approach to find maximum of minimum for every window size in a given array
JavaScript
const maxMinWinSize = arr => {
const res = [];
for (let size = 1; size <= arr.length; size++) {
let maxMin = Number.MIN_SAFE_INTEGER;
for (let i = 0; i <= arr.length - size; i++) {
let min = arr[i];
for (let j = 1; j < size; j++) {
min = Math.min(min, arr[i + j]);
}
maxMin = Math.max(maxMin, min);
}
res.push(maxMin);
}
return res;
};
const array = [1, 2, 3, 5, 1, 7, 3];
console.log(maxMinWinSize(array));
Output[
7, 3, 2, 1,
1, 1, 1
]
Time Complexity: O(N3), where N is the size of the given array.
Auxiliary Space: O(1), constant extra space is being used
Using Stack
In this example we calculate the maximum of minimum values for all window sizes in the given array using stack-based approach. By using stack based approach we find the next smaller and previous smaller of each element and update the maximum of window with size as the difference in their indices.
Example: This JavaScript code is the implementation of the above approach to find maximum of minimum for every window size in a given array
JavaScript
const maxMinWinSize = arr => {
const res = [];
const stack = [];
const left = [];
const right = [];
// Find the next smaller element on the left
for (let i = 0; i < arr.length; i++) {
while (stack.length > 0 && arr[stack[stack.length - 1]] >= arr[i]) {
stack.pop();
}
if (stack.length === 0) {
left.push(-1);
} else {
left.push(stack[stack.length - 1]);
}
stack.push(i);
}
stack.length = 0;
// Find the next smaller element on the right
for (let i = arr.length - 1; i >= 0; i--) {
while (stack.length > 0 && arr[stack[stack.length - 1]] >= arr[i]) {
stack.pop();
}
if (stack.length === 0) {
right[i] = arr.length;
} else {
right[i] = stack[stack.length - 1];
}
stack.push(i);
}
// Initialize result array with -Infinity
for (let i = 0; i <= arr.length; i++) {
res.push(-Infinity);
}
// Calculate max of min for each window size
for (let i = 0; i < arr.length; i++) {
let len = right[i] - left[i] - 1;
res[len] = Math.max(res[len], arr[i]);
}
// Fill in any missing elements in the result array
for (let i = arr.length - 1; i >= 1; i--) {
res[i] = Math.max(res[i], res[i + 1]);
}
return res.slice(1); // Remove the first element (which is -Infinity)
};
const array = [1, 2, 3, 5, 1, 7, 3];
console.log(maxMinWinSize(array));
Output[
7, 3, 2, 1,
1, 1, 1
]
Time Complexity: O(N), where N is the size of the given array.
Auxiliary Space: O(N), for using stack and additional arrays.
Similar Reads
Sliding Window Maximum using JavaScript Given an array of integers and a window size, the task is to find the maximum element in each window of the array as it slides from left to right. Below are the approaches to solve the Sliding Window Maximum problem using JavaScript: Table of Content Brute Force ApproachOptimal ApproachBrute Force A
2 min read
Maximum of all Subarrays of Size k using JavaScript This problem is about finding the biggest number in all groups of k adjacent numbers in an array. In JavaScript, you'd go through the array, looking at each group of k numbers at a time, and picking out the biggest one. This task is pretty common and is useful for things like analyzing data or optim
5 min read
Javascript Program For Rearranging An Array In Maximum Minimum Form - Set 2 (O(1) extra space) Given a sorted array of positive integers, rearrange the array alternately i.e first element should be the maximum value, second minimum value, third-second max, fourth-second min and so on. Examples:Input: arr[] = {1, 2, 3, 4, 5, 6, 7} Output: arr[] = {7, 1, 6, 2, 5, 3, 4}Input: arr[] = {1, 2, 3, 4
5 min read
Javascript Program for Maximum and Minimum in a square matrix. Given a square matrix of order n*n, find the maximum and minimum from the matrix given. Examples: Input : arr[][] = {5, 4, 9, 2, 0, 6, 3, 1, 8}; Output : Maximum = 9, Minimum = 0 Input : arr[][] = {-5, 3, 2, 4}; Output : Maximum = 4, Minimum = -5 Naive Method : We find maximum and minimum of matrix
3 min read
Javascript Program for Queries to find maximum sum contiguous subarrays of given length in a rotating array Given an array arr[] of N integers and Q queries of the form {X, Y} of the following two types: If X = 1, rotate the given array to the left by Y positions.If X = 2, print the maximum sum subarray of length Y in the current state of the array. Examples:Â Input: N = 5, arr[] = {1, 2, 3, 4, 5}, Q = 2,
5 min read
Javascript Program to Find maximum element of each row in a matrix Given a matrix, the task is to find the maximum element of each row.Examples: Input : [1, 2, 3] [1, 4, 9] [76, 34, 21]Output :3976Input : [1, 2, 3, 21] [12, 1, 65, 9] [1, 56, 34, 2]Output :216556Approach : Approach is very simple. The idea is to run the loop for no_of_rows. Check each element inside
2 min read