This is my code for problem C of Codeforces Round 964 (Div. 4), it was a pretty straightforward but i was going out of bounds
and ended up submitting wrong code twice, The interesting part is not even my sublime and codechef ides were able to detect it, below is my implementation and how the codeforces and other ides give different outputs
#include <bits/stdc++.h>
#define int long long
#define endl "\n"
using namespace std;
void solve(){
int n,s,m;
cin>>n>>s>>m;
vector<pair<int,int>>v;
v.push_back({0,0});
for(int i=0;i<n;++i){
int l, r; cin>>l>>r;
v.push_back({l,r});
}
v.push_back({m,m});
sort(v.begin(),v.end());
for(int i=0;i<=n+1;++i){
if(v[i+1].first-v[i].second>=s){
cout<<v[i+1].first<<endl;
cout<<"YES"<<endl;
return;
}
}
cout<<"NO"<<endl;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int t; cin>>t;
while(t--) solve();
return 0;
}
it will give output on codeforces ide as
2053669231248 YES
on test case
1 3 3 10 1 2 3 5 6 8
and same code is giving output as
NO on other online ides and sublime
why this strange anamoly can anyone explain? SlavicG
Auto comment: topic has been updated by dummyactforaskingdoubts (previous revision, new revision, compare).
at i=n+1 i+1 will be outside of array index causing undefined behavior
i understand that but i am just showing how codeforces online ide is different and more accurate from other online ides
it is undefined behavior here, the result is unpredictable, it might even give you a different number if you run it again