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.
You need to find the max of the K values.
Then while you do not will pop the max value, and not arrive the end of queue, continue comparing to the next value, printing the max and pop_front of queue.
If the value to pop is the max value, then start again and find the max of the K values.
This is not optimized and if a lot of max values are repeated the complexity will increment.
But this code pass all the tests in time.
voidprintKMax(intarr[],intn,intk){//Write your code here.deque<int>fila;deque<int>::iteratoritr;intmax=0;for(inti=0;i<n;i++){fila.push_back(arr[i]);}while(fila.size()>=k){itr=fila.begin();for(inti=0;i<k;i++){if(*itr>max)max=*itr;itr++;}cout<<max<<" ";while((fila.front()!=max)&&(itr!=fila.end())){if(max<*itr)max=*itr;cout<<max<<" ";itr++;fila.pop_front();}fila.pop_front();max=0;}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 →
You need to find the max of the K values. Then while you do not will pop the max value, and not arrive the end of queue, continue comparing to the next value, printing the max and pop_front of queue. If the value to pop is the max value, then start again and find the max of the K values.
This is not optimized and if a lot of max values are repeated the complexity will increment. But this code pass all the tests in time.