Maximum of all subarrays of size k | Practice | GeeksforGeeks

Given an array arr[] of size N and an integer K. Find the maximum for each and every contiguous subarray of size K.
Example 1:
Input:
N = 9, K = 3
arr[] = 1 2 3 1 4 5 2 3 6
Output: 
3 3 4 5 5 5 6 
Explanation: 
1st contiguous subarray = {1 2 3} Max = 3
2nd contiguous subarray = {2 3 1} Max = 3
3rd contiguous subarray = {3 1 4} Max = 4
4th contiguous subarray = {1 4 5} Max = 5
5th contiguous subarray = {4 5 2} Max = 5
6th contiguous subarray = {5 2 3} Max = 5
7th contiguous subarray = {2 3 6} Max = 6
Example 2:
Input:
N = 10, K = 4
arr[] = 8 5 10 7 9 4 15 12 90 13
Output: 
10 10 10 15 15 90 90
Explanation: 
1st contiguous subarray = {8 5 10 7}, Max = 10
2nd contiguous subarray = {5 10 7 9}, Max = 10
3rd contiguous subarray = {10 7 9 4}, Max = 10
4th contiguous subarray = {7 9 4 15}, Max = 15
5th contiguous subarray = {9 4 15 12}, 
Max = 15
6th contiguous subarray = {4 15 12 90},
Max = 90
7th contiguous subarray = {15 12 90 13}, 
Max = 90

Your Task:  
You dont need to read input or print anything. Complete the function max_of_subarrays() which takes the array, N and K as input parameters and returns a list of integers denoting the maximum of every contiguous subarray of size K.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(k)
Constraints:
1 ≤ N ≤ 107
1 ≤ K ≤ N
0 ≤ arr[i] ≤ 107
0
vvwlolwhat8 hours ago
    static ArrayList <Integer> max_of_subarrays(int a[], int n, int k)   {       ArrayList<Integer> maxList = new ArrayList<>();       int max;       for (int i = 0; i < n - k + 1; i++) {           max = Integer.MIN_VALUE;           for (int j = i; j < k + i; j++) {               if (a[j] > max) max = a[j];           }           maxList.add(max);       }       return maxList;   }
0
mohitpandey38915 hours ago
class Solution
{
  public:
    
    vector <int> max_of_subarrays(int *arr, int n, int k)
    {
        vector<int> ans;
        list<int> l;
        int i=0;
        int j=0;
        while(j<n){
            while(!l.empty()&&l.back()<arr[j]){
                l.pop_back();
            }
            l.push_back(arr[j]);
            if(j-i+1==k){
                ans.push_back(l.front());
                if(arr[i]==l.front()){
                    l.pop_front();
                }
                i++;
            }
            j++;
        }
        return ans;
    }
};
0
shishir17pandey2 days ago
 vector <int> max_of_subarrays(int *arr, int n, int k)   {       //       multiset<int,greater<int>>s;       vector<int>v;       for(int i=0;i<k;i++){           s.insert(arr[i]);       }        v.push_back(*s.begin());       for(int i=k;i<n;i++){           s.erase(s.lower_bound(arr[i-k]));           s.insert(arr[i]);           v.push_back(*s.begin());       }              return v;   }
0
priyxnshi2 days ago
it fails after 94 test cases, can someone help me optimize the code?
 vector <int> max_of_subarrays(int *arr, int n, int k)   {       // your code here       vector<int>ans;       for(int i=0;i<=n-k;i++){           int max=arr[i];           for(int j=1;j<k;j++){               if(arr[i+j]>max){                   max=arr[i+j];               }           }           ans.push_back(max);       }       return ans;   }
0
shrutidsms2 days ago
can somebody please tell me what's wrong with this code
ArrayList<Integer> ans = new ArrayList<>();
        
        int start = 0;
        int end = k-1;
        int max = Integer.MIN_VALUE;
        
        while( end < n){
            for(int i = start; i < end; i++){
                max = Math.max(Math.max(arr[i], arr[i+1]), max);
            }
            ans.add(max);
            start++;
            end++;
        }
        return ans;
0
nabilmurshedpkt
This comment was deleted.
0
nabilmurshedpkt
This comment was deleted.
-3
yashbiyani3 days ago
100% Accuracy | JAVA SOLUTION
class Solution
{
    //Function to find maximum of each subarray of size k.
    static ArrayList <Integer> max_of_subarrays(int arr[], int n, int k)
    {
        // Your code here
        ArrayList<Integer> list=new ArrayList<Integer>();
        int max=0;
        for(int i=0;i<=n-k;i++)
        {
            max=arr[i];
            for(int j=i+1;j<i+k;j++)
            {
                max=Math.max(arr[j],max);
            }
            list.add(max);
        }
        return list;
    }
}
 
0
dmahindrakar054 days ago
//Java //sliding window
static ArrayList <Integer> max_of_subarrays(int arr[], int n, int k)   {       int i=0,j=0;       ArrayList <Integer> res = new ArrayList<Integer>();       int count =0;              while(j<=n){           if(count<k){               count++;               j++;           }else if(count==k){               int m =i;               int nn =j;               //System.out.println(m+" "+nn);               int max =0;              while(m<nn){                  max =Math.max(max,arr[m]);                  m++;              }               res.add(max);               count++;               j++;           }else{               count--;               i++;           }       }       return res;   }
 
0
sumitsinghrajput786gwalior5 days ago
// Java
 
static ArrayList <Integer> max_of_subarrays(int arr[], int n, int k)   {       // Your code here       ArrayList<Integer> ans = new ArrayList<>();       PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());       int i=0, j=0;       while(i<n){           pq.add(arr[i]);           if(i-j==k-1){               ans.add(pq.peek());               i++;           }           else if(i-j<k-1){               i++;           }           else{               while(i-j>k-1 && !pq.isEmpty()){                   pq.remove(arr[j]);                   j++;               }                              ans.add(pq.peek());               i++;           }       }       return ans;   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.