Suminator
SRM 553 · 2012-06-05 · by vexorian
Problem Statement
A Suminator program is evaluated using the following algorithm:
for i = 0 to length(program) - 1 {
if ( program[i] is 0) {
Pop the top two elements from the stack, add them together
and push the result to the top of the stack.
} else {
Push program[i] to the top of the stack.
}
}
Pop the top element of the stack and print it.For example, when executing the program {1}, we first push the 1 to the stack, and then we print it. When executing the program {5,0,1,2,0}, we take the following steps:
- We push the 5 to the top of the stack.
- We pop the top two elements (5 and 0), add them together and push the result (5).
- We push the 1 to the top of the stack.
- We push the 2 to the top of the stack. At this moment, the stack contains the values 5, 1, and 2 (from bottom to top).
- We pop the top two elements (2 and 1), add them together and push the result (3).
- We print the top element of the stack: the number 3. Note that the stack also contains the value 5, which is ignored.
You are given a
Notes
- The return value always fits into an int. This follows from the constraints and the nature of the problem.
Constraints
- program will contain between 1 and 50 elements, inclusive.
- Each element of program will be between -1 and 1000000000 (10^9), inclusive.
- program will contain -1 exactly once.
- wantedResult will be between 1 and 1000000000 (10^9), inclusive.
Statement by TopCoder, Inc. — view the original on the archive.
{7,-1,0}
10
Returns: 3
{100, 200, 300, 0, 100, -1}
600
Returns: 0
{-1, 7, 3, 0, 1, 2, 0, 0}
13
Returns: 0
We can replace the first element with many other values, but 0 is the smallest that achieves the wanted result.
{-1, 8, 4, 0, 1, 2, 0, 0}
16
Returns: -1
It does not matter what value we use in the first element of the array, the result will always be 15.
{7, -1, 3, 0}
3
Returns: -1
Corner case. Subtraction will tell us to replace -1 with 0, but then the result is 10.
{1000000000, 1000000000, 1000000000, 1000000000, -1, 0, 0, 0, 0}
1000000000
Returns: -1
It does not matter what we replace the -1 with, the result will be larger than 1000000000.
Submissions are judged against all 179 archived test cases, of which 6 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class Suminator with a public method int findMissing(vector<int> program, int wantedResult) · 179 test cases · 2 s / 256 MB per case