0
Array loop + push() + pop()
can anyone tell me why I have to add s.length + 2 for solve this? Thanks https://code.sololearn.com/WtyZH1a24lw9/#
2 Réponses
+ 4
When i=0 and i<3
4 gets removed
So s array length becomes 2
So now
i=1 and i<2
And 7 gets removed
So s array length becomes 1
So now
i=2 and i<1 is false ,in order to remove the last element 4 you need to add 2 to s.length
so it would become
i=2 and i<3 and so last element also gets removed
to keep the length of array same store it in a variable like
b=s.length before for loop and
use i<b;
+ 1
@Abhay thanks, now it's very clear