BinaryCalculator
SRM 735 · 2018-06-25 · by majk
Problem Statement
You have a very simple calculator that has a display and two buttons. The buttons are labelled "-2" and "+3". Whenever you press a button, the corresponding operation is applied to the number shown on the display. For example, if the display shows the number 10, pressing the "-2" button changes it to 10-2 = 8, while pressing the "+3" button changes it to 10+3 = 13.
You are given the
Constraints
- a will be between 1 and 100, inclusive.
- b will be between 1 and 100, inclusive.
10 14 Returns: 3
One solution is to press the button +3 to get 13, then -2 to get 11 and finally +3 to get 14. There are other ways of obtaining the same result, but none of them is shorter than 3 presses.
23 23 Returns: 0
As a = b, you don't need to press any buttons.
3 97 Returns: 33
3 98 Returns: 35
3 99 Returns: 32
18 12 Returns: 3
The only optimal solution is to press "-2" three times in a row.
Submissions are judged against all 31 archived test cases, of which 6 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class BinaryCalculator with a public method int minimumSteps(int a, int b) · 31 test cases · 2 s / 256 MB per case