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<bits/stdc++.h>#define FOR(i,s,e) for(long long i=static_cast<long long>(s);i<static_cast<long long>(e);i++)#define ppi pair<pair<int,int>,int>#define pi pair<int,int>#define pb push_back#define S second#define F firsttypedeflonglongll;usingnamespacestd;vector<int>tree;voidbuild(vector<int>&ar){intn=ar.size();tree.resize(n<<1);for(inti=0;i<n;i++){tree[i+n]=ar[i];}for(inti=n-1;i>0;i--){tree[i]=max(tree[i<<1],tree[i<<1|1]);}}voidchange(intind,intval){intn=tree.size()/2;tree[n+ind]=val;for(inti=(n+ind)/2;i>0;i/=2){tree[i]=tree[i*2]+tree[i*2+1];}}intquery(intl,intr){intans=0,n=tree.size()/2;for(l=l+n,r=r+n;l<r;l/=2,r/=2){if(r%2)ans=max(ans,tree[--r]);if(l%2)ans=max(ans,tree[l++]);}returnans;}voidf(){intn,k;cin>>n>>k;vector<int>v(n);FOR(i,0,n)cin>>v[i];build(v);for(inti=0;i<n-k+1;i++){cout<<query(i,i+k)<<" ";}}intmain(){ios_base::sync_with_stdio(false);cin.tie(nullptr);intt=1;cin>>t;while(t--){f();cout<<endl;}return0;}
voidprintKMax(intarr[],intn,intk){//Write your code here.deque<int>deck(arr,arr+k);// First sublistintmax=deck[0];for(inti=1;i<k;i++)if(deck[i]>max)max=deck[i];cout<<max<<" ";// Following sublistsfor(inti=k;i<n;i++){// Insert new element into sublistdeck.push_back(arr[i]);// Recalculate max over the all deque only if you're about to pop previous max elementif(deck.front()==max){max=deck[1];for(inti=2;i<k+1;i++)if(deck[i]>max)max=deck[i];}// Otherwise just check against last added elementelseif(deck.back()>max)max=deck.back();// Pop front elementdeck.pop_front();cout<<max<<" ";}cout<<endl;}
Hello guys,
I had several problems about performances so I want to share my solution
Code starts by finding the max of first k number. Instead of looking all the numbers over over again. Function checks three different situation.
If the new number of the set, i+1 th element if the ith element is the last element of the first set, bigger than the maximum number directly change to maximum to that.
If not, it checks whether the maximum is the number that is not in the set anymore if not keep the maximum same. and if those two condition false it checks one by one for the new maximum.
void printKMax(int arr[], int n, int k){
//Write your code here.
// n is the length of the arr
// k is the size of the subset of the array
int max=0;
for(int i =0;i
you can cheak this https://github.com/user23052036/Hacker_Rank git repo for all the Hacker Rank solution.
BEST OF LUCK
this is my C++ SOLUTION
include
using namespace std;
template Iterator max_it(Iterator a, Iterator b) { return (*a >= *b) ? a : b; }
int main() { int tt; cin>>tt;
}
here's my c++ 20 code:
This passes all tests:
Hello guys, I had several problems about performances so I want to share my solution
Code starts by finding the max of first k number. Instead of looking all the numbers over over again. Function checks three different situation. If the new number of the set, i+1 th element if the ith element is the last element of the first set, bigger than the maximum number directly change to maximum to that. If not, it checks whether the maximum is the number that is not in the set anymore if not keep the maximum same. and if those two condition false it checks one by one for the new maximum.
void printKMax(int arr[], int n, int k){ //Write your code here. // n is the length of the arr // k is the size of the subset of the array int max=0; for(int i =0;i
}
Why has my code performance problems?