Deque Template:
std::deque<value_type>
Declaration:
deque<int> mydeque; //Creates a double-ended queue of deque of int type
Size
int length = mydeque.size(); //Gives the size of the deque
Push
mydeque.push_back(1); //Pushes element at the end
mydeque.push_front(2); //Pushes element at the beginning
Pop
mydeque.pop_back(); //Pops element from the end
mydeque.pop_front(); //Pops element from the beginning
Empty
mydeque.empty() //Returns a boolean value which tells whether the deque is empty or not
Given a set of arrays of size N and an integer K, you have to find the maximum integer for each and every contiguous subarray of size K for each of the given arrays.
Input Format
The 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 the subarray to be used K. This will be followed by the elements of the array Ai.
Constraints
1 <= T <= 1000
1 <= N <= 10000
1 <= K <= N
1 <= Ai <= 10000, where Ai is the ith element in the array A.
Output Format
For each of the contiguous subarrays of size K of each array, you have to print the maximum integer.
Sample Input
2
5 2
3 4 6 3 4
7 4
3 4 5 8 1 4 10
Sample Output
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 the 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 elements of the subarray of size 4 are 8 8 8 10.
std::deque<value_type>
Declaration:
deque<int> mydeque; //Creates a double-ended queue of deque of int type
Size
int length = mydeque.size(); //Gives the size of the deque
Push
mydeque.push_back(1); //Pushes element at the end
mydeque.push_front(2); //Pushes element at the beginning
Pop
mydeque.pop_back(); //Pops element from the end
mydeque.pop_front(); //Pops element from the beginning
Empty
mydeque.empty() //Returns a boolean value which tells whether the deque is empty or not
Given a set of arrays of size N and an integer K, you have to find the maximum integer for each and every contiguous subarray of size K for each of the given arrays.
Input Format
The 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 the subarray to be used K. This will be followed by the elements of the array Ai.
Constraints
1 <= T <= 1000
1 <= N <= 10000
1 <= K <= N
1 <= Ai <= 10000, where Ai is the ith element in the array A.
Output Format
For each of the contiguous subarrays of size K of each array, you have to print the maximum integer.
Sample Input
2
5 2
3 4 6 3 4
7 4
3 4 5 8 1 4 10
Sample Output
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 the 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 elements of the subarray of size 4 are 8 8 8 10.
#include <bits/stdc++.h>
#include <deque>
using namespace std;
void printKMax(int arr[], int n, int k)
{
deque<int> De_que;
int i;
for (i = 0; i < k; i++)
{
while (!De_que.empty() && arr[i] >= arr[De_que.back()])
{
De_que.pop_back();
}
De_que.push_back(i);
}
for (i = k; i < n; i++)
{
cout << arr[De_que.front()] << " ";
while (!De_que.empty() && De_que.front() <= i - k)
{
De_que.pop_front();
}
while (!De_que.empty() && arr[i] >= arr[De_que.back()])
{
De_que.pop_back();
}
De_que.push_back(i);
}
cout << arr[De_que.front()] << endl;
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int n, k;
cin >> n >> k;
int i;
int *arr = new int[n];
for (i = 0; i < n; i++)
cin >> arr[i];
printKMax(arr, n, k);
}
return 0;
}
The Output of Deque STL Hackerrank Solution
Similar to Deque STL
- Mini Max Sum Hackerrank Solution in C++
- Staircase Hackerrank Solution in C
- Attribute Parser Hackerrank Solution in C++
- Inheritance Introduction Hackerrank Solution in C++
- StringStream Hackerrank Solution in C++
- Attribute Parser Hackerrank Solution in C++
- Basic Data Types HackerRank Solution in C++
- For Loop Hackerrank Solution in C++
- Functions in C++ Hackerrank Solution
- Pointer Hackerrank Solution in C++
- Arrays Introduction Hackerrank Solution in C++
- Strings Hackerrank Solution in C++
- Plus Minus Hackerrank Solution C++
- Hello World in C Hackerrank Solution
- Divisible Sum Pairs Hackerrank Solution in C++
- Apple And Orange Hackerrank Solution
- Simple Array Sum Hackerrank Solution C++
- Compare The Triplets Hackerrank Solution C++
0 Comments: