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.
#include<cmath>#include<cstdio>#include<vector>#include<iostream>#include<algorithm>#include<deque>usingnamespacestd;vector<vector<int>>makesubarray(deque<int>dq,intk){vector<vector<int>>V;for(inti=0;i<=dq.size()-k;i++){// Change to <= to ensure valid subarrayvector<int>v;for(intj=0;j<k;j++){v.push_back(dq[i+j]);}V.push_back(v);}returnV;}deque<int>findmax(vector<vector<int>>v){deque<int>dq;for(auto&a:v){intmax=a[0];// Initialize max with the first element of the subarrayfor(inti=1;i<a.size();i++){// Start from the second elementif(a[i]>max){max=a[i];// Update max if current element is greater}}dq.push_back(max);}returndq;}intmain(){intt;cin>>t;for(inti=0;i<t;i++){intn,k;cin>>n>>k;deque<int>dq;for(intj=0;j<n;j++){intelement;cin>>element;dq.push_back(element);}for(auto&i:findmax(makesubarray(dq,k))){cout<<i<<" ";// Add space for better formatting}cout<<endl;}return0;}
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;}
void printKMax(int arr[], int n, int k){
// //Write your code here.
// int i = 0;
// int j = k-1;
// while(jmaxi) maxi = arr[k];
// }
// cout< dq;
// Process the first 'k' elements.
for (int i = 0; i < k; i++) {
// Remove elements smaller than the current one.
while (!dq.empty() && arr[i] >= arr[dq.back()]) {
dq.pop_back();
}
dq.push_back(i);
}
// Process the remaining elements.
for (int i = k; i < n; i++) {
// The front of the deque is the largest element of the previous window.
cout << arr[dq.front()] << " ";
// Remove elements that are out of this window.
while (!dq.empty() && dq.front() <= i - k) {
dq.pop_front();
}
// Remove elements smaller than the current one.
while (!dq.empty() && arr[i] >= arr[dq.back()]) {
dq.pop_back();
}
dq.push_back(i);
}
// Print the maximum of the last window.
cout << arr[dq.front()] << endl;
}
int main(){
int t;
cin >> t;
while(t>0) {
int n,k;
cin >> n >> k;
int i;
int arr[n];
for(i=0;i<n;i++)
cin >> arr[i];
printKMax(arr, n, k);
t--;
}
return 0;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
include
include
using namespace std;
void printKMax(int arr[], int n, int k){ deque deq; int i=0; deque::iterator it; for (i = 0; i < k; ++i) { while (!deq.empty() && arr[i] >= arr[deq.back()]) { deq.pop_back(); } deq.push_back(i); } for (; i < n; ++i) { cout << arr[deq.front()] << " "; while (!deq.empty() && deq.front() <= i - k) { deq.pop_front(); } while (!deq.empty() && arr[i] >= arr[deq.back()]) { deq.pop_back(); } deq.push_back(i); } cout << arr[deq.front()] << endl; }
int main(){
}
this also exceeded the time limit
`
This is my code which is fine but gives my "exceeded time limits", how can I solve this ?
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.
include
include
using namespace std;
void printKMax(int arr[], int n, int k){ // //Write your code here. // int i = 0; // int j = k-1; // while(jmaxi) maxi = arr[k]; // } // cout< dq;
}
int main(){
}