Sliding Window Maximum using JavaScript Last Updated : 15 May, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 ApproachThe brute force approach involves iterating through all possible windows of size k and finding the maximum element in each window. A function "maxSlidingWindowBruteForce" takes an array "nums" and a window size "k", iterating through "nums" to find maximum elements in each window using "Math.max". These maximums are stored in the "result" array and then returned. Example: The example below shows Sliding Window Maximum using JavaScript using the Brute Force Approach. JavaScript function maxSlideWinBF(nums, k) { const result = []; for (let i = 0; i <= nums.length - k; i++) { let max = Number.MIN_SAFE_INTEGER; for (let j = i; j < i + k; j++) { max = Math.max(max, nums[j]); } result.push(max); } return result; } const nums = [1, 3, -1, -3, 5, 3, 6, 7]; const k = 3; console.log(maxSlideWinBF(nums, k)); Output[ 3, 3, 5, 5, 6, 7 ] Optimal ApproachThe optimal approach utilizes a deque (double-ended queue) to efficiently find the maximum element in each window as it slides from left to right. Define a function "maxSlidingWindowOptimal" with parameters "nums" and "k". Initialize an empty "result" array to store maximum elements. Use a deque to maintain indices of elements in decreasing order. Iterate through "nums", updating the deque according to the algorithm. Store max element based on deque's first element, push to "result". Example: The example below shows Sliding Window Maximum using JavaScript using the Optimal Approach. JavaScript function maxSlideWinOpt(nums, k) { const result = []; const deque = []; for (let i = 0; i < nums.length; i++) { while (deque.length && nums[i] >= nums[deque[deque.length - 1]]) { deque.pop(); } deque.push(i); if (i - deque[0] + 1 > k) { deque.shift(); } if (i >= k - 1) { result.push(nums[deque[0]]); } } return result; } const nums = [1, 3, -1, -3, 5, 3, 6, 7]; const k = 3; console.log(maxSlideWinOpt(nums, k)); Output[ 3, 3, 5, 5, 6, 7 ] Time Complexity: O(n), where n is the length of the input array nums. Space Complexity: O(k), where k is the size of the sliding window. Comment More infoAdvertise with us Next Article Max Length of Subarray with Given Sum Limit in JavaScript Array M mailgew6ge Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Program Similar Reads 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 to Find Maximum of Minimum for Every Window Size in a Given Array 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 4 min read Max Length of Subarray with Given Sum Limit in JavaScript Array Given an array, our task is to find the maximum length of a subarray whose sum does not exceed a given value. We can use different approaches like the Brute Force Approach and the Sliding Window Approach to find the maximum length of a subarray. Below are the approaches to find the maximum length of 3 min read JavaScript Program to Find Top k Elements Given an array, our task is to find the top k elements in an unsorted array of integers in JavaScript, either the largest or the smallest. Duplicate values may be in the array. The output will be a new array with the top k elements arranged in a non-ascending order (from largest to smallest). Table 4 min read JavaScript Program to Construct Largest Number from Digits In this article, we have given a set of digits, and our task is to construct the largest number generated through the combination of these digits. Below is an example for a better understanding of the problem statement. Example: Input: arr[] = {4, 9, 2, 5, 0}Output: Largest Number: 95420Table of Con 3 min read Detecting the Largest Element in an Array of Numbers (nested) in JavaScript Given a nested array of numbers, we need to find and return the largest element present in the array. Below are the approaches to detecting the largest element in an array of Numbers (nested) in JavaScript: Table of Content Iterative ApproachRecursive ApproachIterative ApproachUse a nested loop to t 3 min read Like