Connection Status:
Competition Arena > NumberGuesser
SRM 168 · 2003-10-21 · by schveiguy · Math, Simulation
Class Name: NumberGuesser
Return Type: int
Method Name: guess
Arg Types: (string)
Problem Statement

Problem Statement

Select two numbers between 1 and 9998, inclusive, which have the same exact group of non-zero digits, but are not the same number. For example, you could use 1234 and 4321, or 91 and 901. Now, subtract the smaller of the two numbers from the larger. Finally, pick one of the non-zero digits in the result, and remove the digit from the number. If the resulting number is less than 100, prepend enough zeros so that it has 3 digits. It turns out, that given the remaining 3 digits, one can always determine the digit removed. (See examples for clarification)

You will be given a String leftOver, which contains the three digits left over after the above algorithm is run. You must return the digit which was removed.

Notes

  • Although it may not be obvious, there is only one answer for each input.
  • You can only remove a non-zero digit when running the algorithm.
  • HINT: work backwards from the input.

Constraints

  • leftOver will consist of exactly 3 characters.
  • Each character of leftOver will be a digit '0'-'9'.
  • The input will be possible to acheive by performing the algorithm stated above.
Examples
0)
"087"
Returns: 3

Take the number 4321 and subtract 1234, you get 3087. Remove the 3, and the resulting digits are 087.

1)
"099"
Returns: 9

One possible way to achieve this is by using the numbers 1000 and 1.

2)
"191"
Returns: 7

4525 - 2554 = 1971 also, you could get this with: 1900 - 109 = 1791 or 7900 - 709 = 7191

3)
"009"
Returns: 9
4)
"689"
Returns: 4

Submissions are judged against all 71 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class NumberGuesser with a public method int guess(string leftOver) · 71 test cases · 2 s / 256 MB per case

Submitting as anonymous