Skip to content
Related Articles

Related Articles

Improve Article
Sliding Window Maximum (Maximum of all subarrays of size k) using stack in O(n) time
  • Difficulty Level : Medium
  • Last Updated : 06 Jun, 2021

Give an array arr[] of N integers and another integer k ≤ N. The task is to find the maximum element of every sub-array of size k.

Examples: 

Input: arr[] = {9, 7, 2, 4, 6, 8, 2, 1, 5}
 k = 3
Output: 9 7 6 8 8 8 5
Explanation:
Window 1: {9, 7, 2}, max = 9
Window 2: {7, 2, 4}, max = 7
Window 3: {2, 4, 6}, max = 6
Window 4: {4, 6, 8}, max = 8
Window 5: {6, 8, 2}, max = 8
Window 6: {8, 2, 1}, max = 8
Window 7: {2, 1, 5}, max = 5

Input: arr[] = {6, 7, 5, 2, 1, 7, 2, 1, 10}
 k = 2
Output: 7 7 5 2 7 7 2 10
Explanation:
Window 1: {6, 7}, max = 7
Window 2: {7, 5}, max = 7
Window 3: {5, 2}, max = 5
Window 4: {2, 1}, max = 2
Window 5: {1, 7}, max = 7
Window 6: {7, 2}, max = 7
Window 7: {2, 1}, max = 2
Window 8: {1, 10}, max = 10

Prerequisite: Next greater element

Approach: 
For every index calculate the index upto which the current element is maximum when the subarray starts from this index, i.e For every index i an index j ≥ i such that max(a[i], a[i + 1], … a[j]) = a[i]. Lets call it max_upto[i]
Then the maximum element in the sub-array of length k starting from ith index, can be found by checking every index starting from i to i + k – 1 for which max_upto[i] ≥ i + k – 1 (last index of that window). 

Stack data-structure can be used to store the values in an window, i.e. the last visited or the previous inserted element will be at the top (element with closest index to current element). 



Algorithm:  

  1. Create an array max_upto and a stack to store indices. Push 0 in the stack.
  2. Run a loop from index 1 to index n-1.
  3. Pop all the indices from the stack, which elements (array[s.top()]) is less than the current element and update max_upto[s.top()] = i – 1 and then insert i in the stack.
  4. Pop all the indices from the stack and assign max_upto[s.top()] = n – 1.
  5. Create a variable j = 0
  6. Run a loop from 0 to n – k, loop counter is i
  7. Run a nested loop until j < i or max_upto[j] < i + k – 1, increment j in every iteration.
  8. Print the jth array element.

Implementation:  

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to print the maximum for
// every k size sub-array
void print_max(int a[], int n, int k)
{
    // max_upto array stores the index
    // upto which the maximum element is a[i]
    // i.e. max(a[i], a[i + 1], ... a[max_upto[i]]) = a[i]
 
    int max_upto[n];
 
    // Update max_upto array similar to
    // finding next greater element
    stack<int> s;
    s.push(0);
    for (int i = 1; i < n; i++) {
        while (!s.empty() && a[s.top()] < a[i]) {
            max_upto[s.top()] = i - 1;
            s.pop();
        }
        s.push(i);
    }
    while (!s.empty()) {
        max_upto[s.top()] = n - 1;
        s.pop();
    }
    int j = 0;
    for (int i = 0; i <= n - k; i++) {
 
        // j < i is to check whether the
        // jth element is outside the window
        while (j < i || max_upto[j] < i + k - 1)
            j++;
        cout << a[j] << " ";
    }
    cout << endl;
}
 
// Driver code
int main()
{
    int a[] = { 9, 7, 2, 4, 6, 8, 2, 1, 5 };
    int n = sizeof(a) / sizeof(int);
    int k = 3;
    print_max(a, n, k);
 
    return 0;
}

Java




// Java implementation of the approach
import java.util.*;
 
class GFG
{
 
    // Function to print the maximum for
    // every k size sub-array
    static void print_max(int a[], int n, int k)
    {
        // max_upto array stores the index
        // upto which the maximum element is a[i]
        // i.e. max(a[i], a[i + 1], ... a[max_upto[i]]) = a[i]
 
        int[] max_upto = new int[n];
 
        // Update max_upto array similar to
        // finding next greater element
        Stack<Integer> s = new Stack<>();
        s.push(0);
        for (int i = 1; i < n; i++)
        {
            while (!s.empty() && a[s.peek()] < a[i])
            {
                max_upto[s.peek()] = i - 1;
                s.pop();
            }
            s.push(i);
        }
        while (!s.empty())
        {
            max_upto[s.peek()] = n - 1;
            s.pop();
        }
        int j = 0;
        for (int i = 0; i <= n - k; i++)
        {
 
            // j < i is to check whether the
            // jth element is outside the window
            while (j < i || max_upto[j] < i + k - 1)
            {
                j++;
            }
            System.out.print(a[j] + " ");
        }
        System.out.println();
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int a[] = {9, 7, 2, 4, 6, 8, 2, 1, 5};
        int n = a.length;
        int k = 3;
        print_max(a, n, k);
 
    }
}
 
// This code has been contributed by 29AjayKumar

Python3




# Python3 implementation of the approach
 
# Function to print the maximum for
# every k size sub-array
def print_max(a, n, k):
     
    # max_upto array stores the index
    # upto which the maximum element is a[i]
    # i.e. max(a[i], a[i + 1], ... a[max_upto[i]]) = a[i]
 
    max_upto=[0 for i in range(n)]
 
    # Update max_upto array similar to
    # finding next greater element
    s=[]
    s.append(0)
 
    for i in range(1,n):
        while (len(s) > 0 and a[s[-1]] < a[i]):
            max_upto[s[-1]] = i - 1
            del s[-1]
         
        s.append(i)
 
    while (len(s) > 0):
        max_upto[s[-1]] = n - 1
        del s[-1]
 
    j = 0
    for i in range(n - k + 1):
 
        # j < i is to check whether the
        # jth element is outside the window
        while (j < i or max_upto[j] < i + k - 1):
            j += 1
        print(a[j], end=" ")
    print()
 
# Driver code
 
a = [9, 7, 2, 4, 6, 8, 2, 1, 5]
n = len(a)
k = 3
print_max(a, n, k)
 
# This code is contributed by mohit kumar

C#




// C# implementation of the approach
using System;
using System.Collections.Generic;
 
class GFG
{
 
    // Function to print the maximum for
    // every k size sub-array
    static void print_max(int []a, int n, int k)
    {
        // max_upto array stores the index
        // upto which the maximum element is a[i]
        // i.e. max(a[i], a[i + 1], ... a[max_upto[i]]) = a[i]
 
        int[] max_upto = new int[n];
 
        // Update max_upto array similar to
        // finding next greater element
        Stack<int> s = new Stack<int>();
        s.Push(0);
        for (int i = 1; i < n; i++)
        {
            while (s.Count!=0 && a[s.Peek()] < a[i])
            {
                max_upto[s.Peek()] = i - 1;
                s.Pop();
            }
            s.Push(i);
        }
        while (s.Count!=0)
        {
            max_upto[s.Peek()] = n - 1;
            s.Pop();
        }
        int j = 0;
        for (int i = 0; i <= n - k; i++)
        {
 
            // j < i is to check whether the
            // jth element is outside the window
            while (j < i || max_upto[j] < i + k - 1)
            {
                j++;
            }
            Console.Write(a[j] + " ");
        }
        Console.WriteLine();
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int []a = {9, 7, 2, 4, 6, 8, 2, 1, 5};
        int n = a.Length;
        int k = 3;
        print_max(a, n, k);
 
    }
}
 
// This code has been contributed by 29AjayKumar

Javascript




<script>
 
// Javascript implementation of the approach
 
// Function to print the maximum for
// every k size sub-array
function print_max(a, n, k)
{
     
    // max_upto array stores the index
    // upto which the maximum element is a[i]
    // i.e. max(a[i], a[i + 1], ... a[max_upto[i]]) = a[i]
    let max_upto = new Array(n);
 
    // Update max_upto array similar to
    // finding next greater element
    let s = [];
    s.push(0);
     
    for(let i = 1; i < n; i++)
    {
        while (s.length != 0 &&
           a[s[s.length - 1]] < a[i])
        {
            max_upto[s[s.length - 1]] = i - 1;
            s.pop();
        }
        s.push(i);
    }
     
    while (s.length != 0)
    {
        max_upto[s[s.length - 1]] = n - 1;
        s.pop();
    }
    let j = 0;
    for(let i = 0; i <= n - k; i++)
    {
         
        // j < i is to check whether the
        // jth element is outside the window
        while (j < i || max_upto[j] < i + k - 1)
        {
            j++;
        }
        document.write(a[j] + " ");
    }
    document.write("<br>");
}
 
// Driver code
let a = [ 9, 7, 2, 4, 6, 8, 2, 1, 5 ];
let n = a.length;
let k = 3;
 
print_max(a, n, k);
 
// This code is contributed by patel2127
 
</script>
Output: 
9 7 6 8 8 8 5

 

Complexity Analysis: 

  • Time Complexity: O(n). 
    Only two traversal of the array is needed. So Time Complexity is O(n).
  • Space Complexity: O(n). 
    Two extra space of size n is required.
     

https://youtu.be/m

-Dqu7csdJk?list=PLqM7alHXFySEQDk2MDfbwEdjd2svVJH9p
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live 




My Personal Notes arrow_drop_up
Recommended Articles
Page :