MasterRayuga's blog

By MasterRayuga, history, 2 years ago, In English

Can someone please tell me why my code is showing this type of undefined behavior

here is the first submission which is getting WA on test 2 (when submitted on c++17) & WA on test 5 (when submitted on c++20)

here is the accepted solution The only thing that I have changed here is

        for(int i=0;i<n;i++){
            cin>>arr[i];
            mp1[arr[i]]++;
            if(mp1[arr[i]]>=k){
                s1.insert(arr[i]);
            }
        }

to

        for(int i=0;i<n;i++){
            cin>>arr[i];
            mp1[arr[i]]++;
        }
        for(int i=0;i<n;i++){
            if(mp1[arr[i]]>=k){
                s1.insert(arr[i]);
            }            
        }
  • Vote: I like it
  • 0
  • Vote: I do not like it

»
2 years ago, # |
  Vote: I like it 0 Vote: I do not like it

comparing your both submission using codeforces' compare feature shows that you changed the upper limit of the for loop in maxiConsecutiveSubarray method

  • »
    »
    2 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    You are right but thats not the cause if i change my for loop in maxiConsecutiveSubarray like that of my last submission its still giving me ac, link to submission, So it's not the main culprit here

»
2 years ago, # |
  Vote: I like it 0 Vote: I do not like it

The reason for the UB is 'out of bounds' on the line 40. Change j < N to j + 1 < N