-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deque STL.cpp
90 lines (62 loc) · 1.98 KB
/
Deque STL.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
Task: 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:
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
To know more about deque, click here
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:
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.
Output Format:
For each of the contiguous subarrays of size N 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
Solution(Partial submission):
#include <iostream>
#include <deque>
using namespace std;
void printKMax(int arr[], int n, int k){
//Write your code here.
int max;
for(int i=0;i<=n-k;i++){
max = arr[i];
for(int j = 1 ; j<k; j++){
if(arr[i+j]>max){
max = arr[i+j];
}
}
cout<<max<<" ";
}
cout<<endl;
}
int main(){
int t;
cin >> t;
while(t>0) {
int n,k;
cin >> n >> k;
int arr[n];
for(int i=0;i<n;i++)
cin >> arr[i];
printKMax(arr, n, k);
t--;
}
return 0;
}