Statistics

Problem Statement for "Apothecary"

Problem Statement

An accurate scale is one of the most important tools of the apothecary (an old-time pharmacist). To measure the weight of an object, the apothecary places the object on one pan of the scale, along with some weights of known size, and adds more weights of known size to the other pan until the scales balance. For example, if an object weighs 17 grains, the apothecary could balance the scales by placing a 1-grain weight and a 9-grain weight in the pan with the object, and a 27-grain weight in the other pan.

The apothecary owns weights in a range of sizes starting at 1 grain. In particular, he owns one weight for each power of 3: 1 grain, 3 grains, 9 grains, 27 grains, etc. Determine, for an object weighing W grains, how to distribute the weights among the pans to balance the object. This distribution will be unique. Return a int[] of the weights used. The sign of each weight should be negative if the weight goes in the same pan as the object, and positive if it goes in the other pan. The int[] should be arranged in increasing order.

Definition

Class:
Apothecary
Method:
balance
Parameters:
int
Returns:
int[]
Method signature:
int[] balance(int W)
(be sure your method is public)

Constraints

  • W is between 1 and 1000000, inclusive.

Examples

  1. 17

    Returns: { -9, -1, 27 }

    The example above.

  2. 1

    Returns: { 1 }

    A 1-grain weight is placed in the pan opposite the object being measured.

  3. 2016

    Returns: { -243, -9, 81, 2187 }

    A 9-grain weight and a 243-grain weight are placed in the pan with the object, and an 81-grain weight and a 2187-grain weight are placed in the opposite pan.

  4. 1000000

    Returns: { -531441, -59049, -6561, -243, -27, 1, 81, 729, 2187, 1594323 }

  5. 2

    Returns: { -1, 3 }

  6. 3

    Returns: { 3 }

  7. 4

    Returns: { 1, 3 }

  8. 5

    Returns: { -3, -1, 9 }

  9. 6

    Returns: { -3, 9 }

  10. 7

    Returns: { -3, 1, 9 }

  11. 8

    Returns: { -1, 9 }

  12. 9

    Returns: { 9 }

  13. 14

    Returns: { -9, -3, -1, 27 }

  14. 22

    Returns: { -9, 1, 3, 27 }

  15. 45

    Returns: { -27, -9, 81 }

  16. 84

    Returns: { 3, 81 }

  17. 156

    Returns: { -81, -9, 3, 243 }

  18. 316

    Returns: { -9, 1, 81, 243 }

  19. 500

    Returns: { -243, -9, -3, -1, 27, 729 }

  20. 911

    Returns: { -81, -9, -1, 3, 27, 243, 729 }

  21. 2045

    Returns: { -243, -9, -1, 3, 27, 81, 2187 }

  22. 4987

    Returns: { -2187, -81, -27, -9, 1, 729, 6561 }

  23. 21345

    Returns: { -729, -27, -9, -3, 243, 2187, 19683 }

  24. 69100

    Returns: { -6561, -2187, -729, -243, -3, 1, 9, 81, 19683, 59049 }

  25. 101010

    Returns: { -59049, -19683, -243, -81, 3, 729, 2187, 177147 }

  26. 278982

    Returns: { -177147, -59049, -19683, -2187, -729, -243, -9, 27, 6561, 531441 }

  27. 402349

    Returns: { -177147, -19683, -81, -9, 1, 3, 27, 2187, 6561, 59049, 531441 }

  28. 654321

    Returns: { -59049, -2187, -243, -81, 3, 729, 6561, 177147, 531441 }

  29. 789123

    Returns: { -729, -9, 3, 27, 81, 243, 2187, 19683, 59049, 177147, 531441 }

  30. 999999

    Returns: { -531441, -59049, -6561, -243, -27, 81, 729, 2187, 1594323 }

  31. 531441

    Returns: { 531441 }

  32. 803158

    Returns: { -531441, -177147, -59049, -19683, -6561, -243, -27, -9, -3, 1, 81, 729, 2187, 1594323 }

  33. 797189

    Returns: { -531441, -177147, -59049, -19683, -6561, -2187, -729, -243, -81, -9, -3, -1, 1594323 }

  34. 94

    Returns: { 1, 3, 9, 81 }

  35. 87629

    Returns: { -81, -9, -3, -1, 243, 2187, 6561, 19683, 59049 }

  36. 931112

    Returns: { -531441, -177147, -19683, -729, -81, -9, -1, 27, 243, 6561, 59049, 1594323 }

  37. 723243

    Returns: { -6561, -729, -9, 3, 81, 2187, 19683, 177147, 531441 }

  38. 2016

    Returns: { -243, -9, 81, 2187 }

  39. 4

    Returns: { 1, 3 }

  40. 1000000

    Returns: { -531441, -59049, -6561, -243, -27, 1, 81, 729, 2187, 1594323 }

  41. 12

    Returns: { 3, 9 }


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: