Deque Template:<\/em><\/li><\/ul>\n\n\n\nstd::deque<value_type>\n<\/code><\/pre>\n\n\n\n- Declaration:<\/em><\/li><\/ul>\n\n\n\n
deque<int> mydeque; \/\/Creates a double ended queue of deque of int type<\/code><\/pre>\n\n\n\n- Size<\/em><\/li><\/ul>\n\n\n\n
int length = mydeque.size(); \/\/Gives the size of the deque<\/code><\/pre>\n\n\n\n- Push<\/em><\/li><\/ul>\n\n\n\n
mydeque.push_back(1); \/\/Pushes element at the end\nmydeque.push_front(2); \/\/Pushes element at the beginning<\/code><\/pre>\n\n\n\n- Pop<\/em><\/li><\/ul>\n\n\n\n
mydeque.pop_back(); \/\/Pops element from the end\nmydeque.pop_front(); \/\/Pops element from the beginning<\/code><\/pre>\n\n\n\n- Empty<\/em><\/li><\/ul>\n\n\n\n
mydeque.empty() \/\/Returns a boolean value which tells whether the deque is empty or not<\/code><\/pre>\n\n\n\nGiven a set of arrays of size N<\/em><\/strong> and an integer K<\/em><\/strong>, you have to find the maximum integer for each and every contiguous subarray of size K<\/em><\/strong> for each of the given arrays.<\/p>\n\n\n\n<\/span>Input Format<\/strong><\/span><\/h2>\n\n\n\nFirst line of input will contain the number of test cases T<\/em>. For each test case, you will be given the size of array N<\/em> and the size of subarray to be used K<\/em>. This will be followed by the elements of the array Ai<\/sub><\/em>.<\/p>\n\n\n\n<\/span>Constraints<\/strong><\/span><\/h2>\n\n\n\n- 1 <= T<\/em> <= 1000<\/strong><\/li>
- 1 <= N<\/em> <= 10000<\/strong><\/li>
- 1 <= K<\/em> <= N<\/em><\/strong> <\/li>
- 1 <= A<\/em>i<\/em><\/sub> <= 10000<\/strong>, where A<\/em>i<\/em><\/sub><\/strong> is the i<\/em>th<\/em><\/sup><\/strong> element in the Array A<\/strong><\/em>.<\/li><\/ul>\n\n\n\n
<\/span>Output Format<\/strong><\/span><\/h2>\n\n\n\nFor each of the contiguous subarrays of size K<\/em><\/strong> of each array, you have to print the maximum integer.<\/p>\n\n\n\nSample Input<\/strong><\/p>\n\n\n\n2\n5 2\n3 4 6 3 4\n7 4\n3 4 5 8 1 4 10<\/code><\/pre>\n\n\n\nSample Output<\/strong><\/p>\n\n\n\n4 6 6 4\n8 8 8 10<\/code><\/pre>\n\n\n\n<\/span>Explanation<\/strong><\/span><\/h2>\n\n\n\nFor 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.<\/p>\n\n\n\n
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.<\/p>\n\n\n\n
<\/span>Solution – Deque-STL in C++<\/strong><\/span><\/h2>\n\n\n\n<\/span>C++<\/strong><\/span><\/h3>\n\n\n\n#include <iostream>\n#include <deque> \nusing namespace std;\n\nvoid printKMax(int arr[], int n, int k){\n\t\/\/Write your code here.\n std::deque<int> dq(k);\n int i;\n for (i = 0; i < k; ++i) {\n while ( (!dq.empty()) && arr[i] >= arr[dq.back()])\n dq.pop_back();\n \n dq.push_back(i);\n }\n \n for ( ; i < n; ++i) {\n cout << arr[dq.front()] << \" \";\n \n while ( (!dq.empty()) && dq.front() <= i - k)\n dq.pop_front();\n \n while ( (!dq.empty()) && arr[i] >= arr[dq.back()])\n dq.pop_back();\n \n dq.push_back(i);\n }\n \n cout << arr[dq.front()] << endl;\n}\n\nint main(){\n \n\tint t;\n\tcin >> t;\n\twhile(t>0) {\n\t\tint n,k;\n \tcin >> n >> k;\n \tint i;\n \tint arr[n];\n \tfor(i=0;i<n;i++)\n \t\tcin >> arr[i];\n \tprintKMax(arr, n, k);\n \tt--;\n \t}\n \treturn 0;\n}<\/pre>\n\n\n\n