We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
voidprintKMax(intarr[],intn,intk){//Write your code here.deque<int>deck(arr,arr+k);// First sublistintmax=deck[0];for(inti=1;i<k;i++)if(deck[i]>max)max=deck[i];cout<<max<<" ";// Following sublistsfor(inti=k;i<n;i++){// Insert new element into sublistdeck.push_back(arr[i]);// Recalculate max over the all deque only if you're about to pop previous max elementif(deck.front()==max){max=deck[1];for(inti=2;i<k+1;i++)if(deck[i]>max)max=deck[i];}// Otherwise just check against last added elementelseif(deck.back()>max)max=deck.back();// Pop front elementdeck.pop_front();cout<<max<<" ";}cout<<endl;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Deque-STL
You are viewing a single comment's thread. Return to all comments →
This passes all tests: