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.
I didn't know if using deque was mandatory, so I went oldschool with some optimizations to avoid a full n² complexity:
voidprintKMax(intarr[],intn,intk){//Write your code here.intmax=0;intpos=0;for(inti=0;i<k;i++){if(arr[i]>max){pos=i;max=arr[pos];}}cout<<max;for(intj=k;j<n;j++){if(j-k<pos){if(arr[j]>max){max=arr[j];pos=j;}}else{max=0;for(inti=1;i<=k;i++){if(arr[j-k+i]>max){pos=j-k+i;max=arr[pos];}}}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 →
I didn't know if using deque was mandatory, so I went oldschool with some optimizations to avoid a full n² complexity: