Statistics

Problem Statement for "BinaryCalculator"

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 ints a and b. The display currently shows the integer a. You would like to change the displayed number to b. Calculate and return the smallest total number of buttons you need to press.

Definition

Class:
BinaryCalculator
Method:
minimumSteps
Parameters:
int, int
Returns:
int
Method signature:
int minimumSteps(int a, int b)
(be sure your method is public)

Constraints

  • a will be between 1 and 100, inclusive.
  • b will be between 1 and 100, inclusive.

Examples

  1. 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.

  2. 23

    23

    Returns: 0

    As a = b, you don't need to press any buttons.

  3. 3

    97

    Returns: 33

  4. 3

    98

    Returns: 35

  5. 3

    99

    Returns: 32

  6. 10

    11

    Returns: 2

  7. 10

    12

    Returns: 4

  8. 10

    13

    Returns: 1

  9. 10

    10

    Returns: 0

  10. 99

    99

    Returns: 0

  11. 90

    89

    Returns: 3

  12. 90

    88

    Returns: 1

  13. 90

    87

    Returns: 4

  14. 80

    10

    Returns: 35

  15. 80

    9

    Returns: 38

  16. 18

    12

    Returns: 3

    The only optimal solution is to press "-2" three times in a row.

  17. 23

    62

    Returns: 13

  18. 2

    4

    Returns: 4

  19. 3

    2

    Returns: 3

  20. 13

    12

    Returns: 3

  21. 1

    3

    Returns: 4

  22. 2

    1

    Returns: 3

  23. 87

    44

    Returns: 24

  24. 10

    15

    Returns: 5

  25. 5

    4

    Returns: 3

  26. 3

    5

    Returns: 4

  27. 2

    5

    Returns: 1

  28. 2

    7

    Returns: 5

  29. 1

    9

    Returns: 6

  30. 6

    1

    Returns: 5

  31. 9

    2

    Returns: 6


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: