Skip to content
the poor coder

Hackerrank Deque-STL Solution

Poor Man
Published
Updated
Reading time
5 min read

Double ended queue or Deque(part of C++ STL) are sequence containers with dynamic sizes that can be expanded or contracted on both ends (either its front or its back). The member functions of deque that are mainly used are:

Deque Template:

Text
std::deque<value_type>

Declaration:

Text
deque<int> mydeque; //Creates a double ended queue of deque of int type

Size

Text
int length = mydeque.size(); //Gives the size of the deque

Push

Text
mydeque.push_back(1); //Pushes element at the end
mydeque.push_front(2); //Pushes element at the beginning

Pop

Text
mydeque.pop_back(); //Pops element from the end
mydeque.pop_front(); //Pops element from the beginning

Empty

Text
mydeque.empty() //Returns a boolean value which tells whether the deque is empty or not

To know more about deque, click here

Given a set of arrays of size  and an integer , you have to find the maximum integer for each and every contiguous subarray of size  for each of the given arrays.

Input Format

First line of input will contain the number of test cases T. For each test case, you will be given the size of array N and the size of subarray to be used K. This will be followed by the elements of the array Ai.

Constraints



, where  is the  element in the array .

Output Format

For each of the contiguous subarrays of size  of each array, you have to print the maximum integer.

Sample Input

Text
2
5 2
3 4 6 3 4
7 4
3 4 5 8 1 4 10

Sample Output

Text
4 6 6 4
8 8 8 10

Explanation

For the first case, the contiguous subarrays of size 2 are {3,4},{4,6},{6,3} and {3,4}. The 4 maximum elements of subarray of size 2 are:  4 6 6 4.

For the second case,the contiguous subarrays of size 4 are {3,4,5,8},{4,5,8,1},{5,8,1,4} and {8,1,4,10}. The 4 maximum element of subarray of size 4 are:  8 8 8 10.

Solution in cpp#

Approach 1.

Python
#include <iostream>
#include <deque> 
using namespace std;
void printKMax(int arr[], int n, int k){
    deque<pair<int, int>> dq;
    for(int a=0; a<n; a++){
        while(!dq.empty() && a - dq.front().second >= k){
            dq.pop_front();
        }
        while(!dq.empty() && dq.back().first <= arr[a]){
            dq.pop_back();
        }
        dq.push_back({arr[a], a});
        if(a >= k - 1){
            cout << dq.front().first << " ";
        }
    }
    cout << "\n";
}
int main(){
  
   int t;
   cin >> t;
   while(t>0) {
      int n,k;
       cin >> n >> k;
       int i;
       int arr[n];
       for(i=0;i<n;i++)
            cin >> arr[i];
       printKMax(arr, n, k);
       t--;
     }
     return 0;
}

Approach 2.

Python
#include <iostream>
#include <deque> 
using namespace std;
void printKMax(int arr[], int n, int k){
    if (n == 0) {
        return;
    }
    
    if (n == 1 && k == 1) {
        cout << arr[1] << "\n";
    }
    
    deque<int> maxs;
    for(int i = 0; i < n; i++) {
        
        while(!maxs.empty() && arr[i] > arr[maxs.back()]) {
            maxs.pop_back();
        }
        maxs.push_back(i);
        
        while(!maxs.empty() && maxs.front() <= (i - k)) {
            maxs.pop_front();
        }

        if (i >= k-1) {
            cout << arr[maxs.front()] << " ";
        }
    }
    cout << "\n";
}

int main(){
  
   int t;
   cin >> t;
   while(t>0) {
      int n,k;
       cin >> n >> k;
       int i;
       int arr[n];
       for(i=0;i<n;i++)
            cin >> arr[i];
       printKMax(arr, n, k);
       t--;
     }
     return 0;
}

Approach 3.

Python
#include <iostream>
#include <deque> 
#include <algorithm>
using namespace std;
void printKMax(int arr[], int n, int k){
   //Write your code here.
deque<int> deq(k); int  i;
    for(i=0;i<k;i++)
        {
        while(!deq.empty()&&arr[i]>=arr[deq.back()])
            {
            deq.pop_back();
        }
        deq.push_back(i);   
    }
    for(i=k;i<n;i++)
        {
        cout<<arr[deq.front()]<<" ";
        while(!deq.empty() && deq.front()<=(i-k))
            {
                deq.pop_front();
            }
        while(!deq.empty()&&arr[i]>=arr[deq.back()])
            {
                deq.pop_back();
            }
        deq.push_back(i);
    }
    cout<<arr[deq.front()]<<endl;
}
int main(){
  
   int t;
   cin >> t;
   while(t>0) {
      int n,k;
       cin >> n >> k;
       int i;
       int arr[n];
       for(i=0;i<n;i++)
            cin >> arr[i];
       printKMax(arr, n, k);
      // printf("\n");
       t--;
     }
     return 0;
}

Keep going

2020

Hackerrank Print Pretty Solution

Your manager gave you a text file with many lines of numbers to format and print. For each row of space-separated doubles, format and print the numbers using the specifications in the Output Format section below. Input…

2020

Hackerrank Maps-STL Solution

Maps are a part of the C++ STL.Maps are associative containers that store elements formed by a combination of a key value and a mapped value, following a specific order.The mainly used member functions of maps are: Map…

2020

Hackerrank Sets-STL Solution

Sets are a part of the C++ STL. Sets are containers that store unique elements following a specific order. Here are some of the frequently used member functions of sets: Declaration: Size: Insert: Erasing an element…

ESC