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;}
`
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 also exceeded the time limit
`