Let’s take the test case n=10 m=1:
So, primes value for this test case will be 0.
Now in your AC solution:
As primes is 0 so the loop will never run, and output ans which was initialized before the loop as int ans=0 is printed 0 as print statement is out of loop.
For failed code:
Again, the value of primes is 0, the loop never runs, but this time we don’t print anything as the print statement is inside the loop and the loop never ran thus skipping an answer.
Your code with return statement is failing when the answer is 0. It prints no answer in that case. Run with the test case given in the problems statement.