Someone please explain me that in the following codes, the first one gave me a wrong answer while the second one gave me an AC. Both the programs are ditto same in logic but i guess i am going wrong somewhere in the concept of vectors.

Please help…

https://www.codechef.com/viewsolution/35691734

This is completely your solution i just added if(k!=n ) because lower might get equal to v.end().

v.end()-v.begin() equals n… Now it didn’t throw RE as a[n] is returning some random value. Note the original vector has no number in nth position.

The first approach shows wrong verdict because when *lower* points to *a.end()*, then the value of *k* is *n* and **a[n]** contains garbage value and there may be an input in which sum of *x* and *y* is equal to that garbage value.

Look at this.

Same case does not occur with the second approach beacause in array a[n] = 0.

You should rename the title of your post. It seems as if there was some issue with the problem.

isn’t he doing the same in second code?

your first submission is contest submission, but second is after constest submission. they sometime change test cases in between.

but for vector a[n] is giving 0,its not throwing any runtime error

Ok, i think i got it.

Thanks a lot!!