#include <iostream> #include <vector> #include <deque> using namespace std; void printMaxSlidingWindow( vector<int> & array, int k ) { deque<int> indQueue; int i; //Initial sliding window for( i = 0; i < k ; i++ ) { //Delete all smaller elements before pushing the current element while( !indQueue.empty() && array[indQueue.back()] <= array[i] ) indQueue.pop_back(); indQueue.push_back(i); } for( ; i < array.size(); i++ ) { cout << array[indQueue.front()] << " "; while( !indQueue.empty() && array[indQueue.back()] <= array[i] ) indQueue.pop_back(); //If previous window element is present, remove it if( !indQueue.empty() && indQueue.front() <= i-k ) indQueue.pop_front(); indQueue.push_back(i); } //print the maximum of last sliding window cout << array[indQueue.front()] << endl; } //Brute force method void printMax( vector<int> & array, int k ) { int i, j; int max = INT_MIN; for( i = 0; i < k; i++ ) { if( max < array[i] ) max = array[i]; } cout << max << " "; for( i = 0; i < array.size()-k; i++ ) { max = array[i]; for( j = i+1; j < i+k; j++ ) { if( max < array[j] ) max = array[j]; } cout << max << " "; } } int main() { int n,k; cin >> n >> k; vector<int> array; int i; for( i = 0; i < n; i++ ) { int num; cin >> num; array.push_back(num); } printMaxSlidingWindow( array, k ); return 0; }