Hello,
I have a query which i faced in Round #829 div 2 question 1754B - Kevin and Permutation. When i took vector of required size it is giving me runtime error that is out of bound . but when i took vector of somewhat bigger size it got accepted.
runtime error submission 180120147
accepted submission 180108219
Why the vector of enough required size giving the error??
The vector with size
n+1
has indices from0
ton
soa[n+1]
isn't correct.when x ==n+1 the loop will terminate. so "i" never becomes i=n+2, as "i" is increasing by 2. as before termination in loop's last execution ,"i+1" is equal to n.
So i think it should work but its giving the error.
Suppose
n=3
, then on 2nd iteration ofwhile
i=3
andx=3
and you are trying to accessa[4]
when your vector has size4
and indexed0,1,2,3
. This means you're trying to access out-of-bounds element which leads toUndefined Behaviour
in C++.P. S. This problem does not require using additional memory ;)
the value of i+1 is going beyond n+1
i changed the code a bit for debugging
for input:
output:
hope this helps