can anybody help me, why I am getting TLE in TEST CASE 9 ,even though time complexity of my code is nlog(n).↵
question C of Educational Codeforces Round 170.↵
CODE↵
int main(){↵
int t;↵
cin>>t;↵
while(t--){↵
ll n,k;cin>>n>>k;↵
unordered_map<ll,ll>mp;↵
set<ll>p;↵
for(int i=0;i<n;i++){↵
ll x;cin>>x; p.insert(x);↵
mp[x]++;↵
}↵
vector<ll>s;↵
for(auto i:p){↵
s.pb(i);↵
}↵
// cout<<s.size()<<endl;↵
ul ans=mp[s[0]],count=mp[s[0]],crr=1,l=0;↵
for(int i=1;i<s.size();i++){↵
if(s[i]-s[i-1]==1 && crr<k){↵
count+=mp[s[i]];↵
crr++;↵
}↵
else if(s[i]-s[i-1]!=1){↵
ans=max(ans,count);↵
count=mp[s[i]];crr=1;↵
l=i;↵
}↵
else{↵
ans=max(ans,count);↵
count-=mp[s[l]];↵
count+=mp[s[i]];↵
l++;↵
}↵
}↵
ans=max(ans,count);↵
cout<<ans<<endl;↵
↵
↵
↵
}↵
return 0;↵
}↵
question C of Educational Codeforces Round 170.↵
CODE↵
int main(){↵
int t;↵
cin>>t;↵
while(t--){↵
ll n,k;cin>>n>>k;↵
unordered_map<ll,ll>mp;↵
set<ll>p;↵
for(int i=0;i<n;i++){↵
ll x;cin>>x; p.insert(x);↵
mp[x]++;↵
}↵
vector<ll>s;↵
for(auto i:p){↵
s.pb(i);↵
}↵
// cout<<s.size()<<endl;↵
ul ans=mp[s[0]],count=mp[s[0]],crr=1,l=0;↵
for(int i=1;i<s.size();i++){↵
if(s[i]-s[i-1]==1 && crr<k){↵
count+=mp[s[i]];↵
crr++;↵
}↵
else if(s[i]-s[i-1]!=1){↵
ans=max(ans,count);↵
count=mp[s[i]];crr=1;↵
l=i;↵
}↵
else{↵
ans=max(ans,count);↵
count-=mp[s[l]];↵
count+=mp[s[i]];↵
l++;↵
}↵
}↵
ans=max(ans,count);↵
cout<<ans<<endl;↵
↵
↵
↵
}↵
return 0;↵
}↵