Test case works when by itself but fails when with multiple test cases

The test case (10, 20 30, 40) yields the expected result 4 just fine when it is the only test case. However, when (10,20,30,40) is one among the ten test cases provided in the question, it gives an incorrect answer of 3.
Please find attached:

  1. code link to codingblocks ide:
  2. link to download the ten test cases:

@heyitsmeadarsh hey Adarsh your code have some issue It now fixed you can check now
your code.

@jaiskid Thank you for your time. I can see that you have altered my code. But, I can not find any real practical differences between yours and my methods of implementation. Could you please explain your reasoning? Particularly, why the test case fails when bunched up with other test cases. Thanks!

@heyitsmeadarsh hey adarsh do dry run with your code and then dry run with changed code.you will get to know

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.