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/#

4th Jun 2020, 9:59 AM
Alessio Farroni
Alessio Farroni - avatar
2 Antworten
+ 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;
4th Jun 2020, 10:12 AM
Abhay
Abhay - avatar
+ 1
@Abhay thanks, now it's very clear
4th Jun 2020, 10:26 AM
Alessio Farroni
Alessio Farroni - avatar