Skip to main content

Maximum of all subarrays of size k

Problem Statement:
Given an array and an integer k, find the maximum for each and every contiguous subarray of size k.
Input:
The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a single integer 'N' denoting the size of array and the size of subarray 'k'.
The second line contains 'N' space-separated integers A1, A2, ..., AN denoting the elements of the array.
Output:
Print the maximum for every subarray of size k.
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 100
1 ≤ k ≤ N
0 ≤A[i]<1000
Example:
Input:
2
9 3
1 2 3 1 4 5 2 3 6
10 4
8 5 10 7 9 4 15 12 90 13
Output:
3 3 4 5 5 5 6
10 10 10 15 15 90 90
Video Tutorial 

Code:

 
import java.util.*; import java.lang.*; import java.io.*; class Subarray { public static void main (String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); for(int u=0;u<t;u++) { int n=sc.nextInt(); int k=sc.nextInt(); int arr[]=new int[n]; //dequeue ArrayList <Integer> al=new ArrayList<Integer>(); //ArrayList to store the result ArrayList <Integer> res=new ArrayList<Integer>(); for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); } //adding first window into dequeue //add indexes to the dequeue for(int i=0;i<k;i++) { if(al.size()==0) { al.add(i); } else { while(al.size()!=0 && arr[i]>arr[al.get(al.size()-1)]) { al.remove(al.size()-1); } al.add(i); } } //deque will be sorted in descending order res.add(arr[al.get(0)]); //for rest of the window process element one by one for(int i=k;i<n;i++) { while(al.size()!=0 && al.get(0)<=i-k) { al.remove(0); } while(al.size()!=0 && arr[i]>arr[al.get(al.size()-1)]) { al.remove(al.size()-1); } al.add(i); res.add(arr[al.get(0)]); } for(int i=0;i<res.size();i++) { System.out.print(res.get(i)+" "); } System.out.println(" "); } } }

Comments

Popular posts from this blog

A Very Basic yet important question in a life of a software developer

 Processes Vs Threads Process:  It is an execution unit where a program runs. The OS helps to create, schedule, and terminates the processes which is used by CPU. The other processes created by the main process are called child process. A process operations can be controlled with the help of PCB(Process Control Block). PCB contains all the crucial information related to processing like process id, priority, state, and contents CPU register, etc. Important properties of the process: - Creation of each process requires separate system calls for each process. - It is an isolated execution entity and does not share data and information. - Processes use the IPC(Inter-Process Communication) mechanism for communication that significantly increases the number of system calls. - Process management takes more system calls. - A process has its stack, heap memory, and data map. Thread:   A process can have multiple threads, all executing at the same time. A thread is lightw...

Count the Reversals

Problem Statement: Given a string  S  consisting only of opening and closing curly brackets  '{'  and  '}'  find out the minimum number of reversals required to make a balanced expression. Input The first line of input contains an integer  T  denoting the number of test cases. Then  T  test cases follow.  The first line of each test case contains a string  S  consisting only of  {  and  } . Output Print out minimum reversals required to make  S  balanced.If it cannot be balanced, then print  -1 . Constraints 1 <=  T  <= 100 0 <    S   <= 50 Examples   Input 4 }{{}}{{{ {{}}}} {{}{{{}{{}}{{ {{{{}}}} Output 3 1 -1 0 Expected Complexity Time :   O(n) Space : O(n) Code: import java.util.*; import java.lang.*; import java.io.*; class GFG { public static void main (String[] args) { Scanner sc=new Scanner(S...

CheatSheet for Searching Algorithms

CheatSheet for Searching Algorithms 1. Linear Search int search(int arr[], int x) { int n = arr.length; for(int i = 0; i < n; i++) { if(arr[i] == x) return i; } return -1; } The time complexity of above algorithm is O(n). 2. Binary Search // Returns location of key, or -1 if not found // Returns location of key, or -1 if not found int BinarySearch(int A[], int l, int r, int key) { int m; while( l <= r ) { m = l + (r-l)/2; if( A[m] == key ) // first comparison return m; if( A[m] < key ) // second comparison l = m + 1; else r = m - 1; } return -1; } The time complexity of Binary Search Theta(log(n)) Auxiliary Space: O(1) in case of iterative implementation. In case of recursive implementation, O(Logn) recursion call stack space. 3. The Ubiquitous Binary Search // Invariant: A[l] <= key and A[r] > key // Boundar...