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.
using namespace std;
void print(vector &arr, int k) {
deque dq;
int n = arr.size();
for (int i = 0; i < n; ++i) {
if (!dq.empty() && dq.front() == i - k)
dq.pop_front();
while (!dq.empty() && arr[dq.back()] < arr[i])
dq.pop_back();
dq.push_back(i);
if (i >= k - 1)
cout << arr[dq.front()] << " ";
}
cout << '\n';
}
int main()
{
// freopen("input.inp", "r", stdin);
// freopen("output.out", "w", stdout);
int t; cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
vector arr(n);
for (int i = 0; i < n; ++i)
cin >> arr[i];
print(arr, k);
}
return 0;
}
//Write your code here.
deque<int> d(arr,arr+k); //sub array
auto max = max_element(d.begin(),d.end()); //get max of sub array
cout << *max <<" ";
//get max if max is removed
if(*max == d.front())
max = max_element(d.begin()+1,d.end());
d.pop_front(); //remove front element
for(int i=k;i<n;++i){
d.push_back(arr[i]);
//check max
if(arr[i] >= *max)
max = d.end()-1;
cout << *max <<" ";
//get max if max is removed
if(max == d.begin()){
max = max_element(d.begin()+1,d.end());
}
d.pop_front();
}
#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;}
include
using namespace std; void print(vector &arr, int k) { deque dq; int n = arr.size();
}
int main() { // freopen("input.inp", "r", stdin); // freopen("output.out", "w", stdout); int t; cin >> t; while (t--) { int n, k; cin >> n >> k; vector arr(n); for (int i = 0; i < n; ++i) cin >> arr[i]; print(arr, k); } return 0; }
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 ?