Deque-STL

  • + 0 comments
    //Write your code here.
    deque<int> d(arr,arr+k); //sub array
    auto max = max_element(d.begin(),d.end()); //get max of sub array
    cout << *max <<" "; 
    
    //get max if max is removed
    if(*max  == d.front())
        max = max_element(d.begin()+1,d.end());
    
    d.pop_front(); //remove front element     
    
    for(int i=k;i<n;++i){
        d.push_back(arr[i]);
        //check max
        if(arr[i] >= *max) 
            max = d.end()-1;              
    
        cout << *max <<" ";
        //get max if max is removed
        if(max  == d.begin()){ 
            max = max_element(d.begin()+1,d.end());
        }  
        d.pop_front();      
    }