I have some problem with an exercise from geeksforgeeks portal. I don't know why they say to me wrong answer. I have completed the function and my results on their visible test cases is good. Maybe i don't understand what they want from me to solve in the problem? Can you help me please?
My code is this:
//Your Code here
long answer = 0;
for (int i = 0; i < str.length(); i++)
{
long multiply = (str.charAt(i) - 48) * answer;
long sum = (str.charAt(i) - 48) + answer;
if (multiply > sum)
answer = multiply;
else if (sum > multiply)
answer = sum;
}
System.out.println(answer);
And as you can see... my solution works for the visible test cases:
Important!!! They don't show the input test cases where the code have failed. They don't show any input of the test cases.
EDITED: some request of custom input to show (also changed a little the if else statement... included now the possibility of equals sum and multiply
Your code contains a small bug due to the following if-else statements:
if (multiply > sum)
answer = multiply;
else if (sum > multiply)
answer = sum;
You forgot the condition when sum equals multiply. In that case, your answer variable remains unchanged in that iteration and you will get a wrong answer finally.
Now sum == multiply can occur for the test case of "22" --> your code gives answer as 2, while the answer should be 4.
This might be one of the test cases failing for your code.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.