+ 2

challenging to find error!!

i recently tried a challenge in java on other website which is seen in this link https://code.dcoder.tech/challenges/5b65902d4bc1dd1052db25d4/stack-stack-stack i tried this problem and at last made this code https://code.sololearn.com/coq9E2a3SQGk/#java but this code doesnt pass in a test case please can anybody suggest what is going wrong here

22nd Apr 2020, 9:29 AM
Rajb957
Rajb957 - avatar
3 Respuestas
+ 1
Change your if blocks to if else if blocks. If 1 condition is true we know the other 2 are false. No reason to test them all for each iteration of the loop. Then change your pop loop to loop until the stack is empty as the size will change as you pop items from the stack. while (!stack.empty()) { sum+=stack.pop(); }
22nd Apr 2020, 10:10 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
ChaoticDawg Jayakrishna🇮🇳 thanks for the reply it works well I liked it
22nd Apr 2020, 3:35 PM
Rajb957
Rajb957 - avatar
+ 1
In last loop, use < instead of <= for(int i=0; i<stack.size(); i++)
22nd Apr 2020, 10:36 AM
Jayakrishna 🇮🇳