• + 1 comment

    I agree with @farhan_tanvir_u1, the way you have it coded. The second stack will be checked to the end, once the first stack has been totally depleted, and the second stack holds all the numbers, it will not stop. Suggest adding an additional check for sum <= x. Once i = 0 && sum > x, you can stop checking the second stack.