actually, it's a great question not silly, so consider n = 192, so if we use if() instead of while, after adding 192 to the list you will increase curr = 193 come to if()==> now here you will check if(193%10==9 || 193>=n) so you will make curr = 193/10 = 19 ==> 19+1 = 20====> so now due to if condition you missed to add value 2, because 2 is supposed to come before 20.... but if you use while loop first you will make 193/10 = 19 and you will again check 19 ===> 19%10 == 9 so again you will make curr = curr/10 ==> 19/10 = 1 and then increase it 1+1 = 2 , now you will not miss value 2.
Came here from leetcode awesome explanation
Thank You very much for clear explanation
Thanks buddy, nice to hear that you liked it.
All clear explanation
Good sir
Thanks bro 👍
a silly question using 'if' instead of 'while' works for the most test cases but fails at some why n how does while makes any difference?
actually, it's a great question not silly, so consider n = 192, so if we use if() instead of while, after adding 192 to the list you will increase curr = 193 come to if()==> now here you will check if(193%10==9 || 193>=n) so you will make curr = 193/10 = 19 ==> 19+1 = 20====> so now due to if condition you missed to add value 2, because 2 is supposed to come before 20....
but if you use while loop first you will make 193/10 = 19 and you will again check 19 ===> 19%10 == 9 so again you will make curr = curr/10 ==> 19/10 = 1 and then increase it 1+1 = 2 , now you will not miss value 2.
@@TheCodeThoughts ah thank u so much great explanation, now I get it ❤️❤️🙏🏻🙏🏻