Statistics

Problem Statement for "StockSales"

Problem Statement

For tax purposes I need to trade some stock, generating as small a positive amount of revenue as possible. I can buy or sell any integer quantity of each stock (I can "sell short", which means that I can sell more of a stock than I have).

Create a class StockSales that contains a method getAmounts that is given a int[] values, the prices of the stocks I am willing to trade, and that returns the amounts of each stock that I should buy or sell. In the return, positive amounts represent amounts to sell, while negatives represent amounts to buy.

To make the answer unique, choose the amounts in order. Choose each amount to have the smallest possible absolute value (considering all the earlier choices). If both the negative and positive amount is available, choose the positive amount.

Definition

Class:
StockSales
Method:
getAmounts
Parameters:
int[]
Returns:
int[]
Method signature:
int[] getAmounts(int[] values)
(be sure your method is public)

Constraints

  • values contains between 1 and 50 elements inclusive.
  • Each element in values is between 1 and 1,000,000 inclusive.

Examples

  1. {6,12,17}

    Returns: { 0, -7, 5 }

    The smallest positive revenue is 1. We can trade 0 of the first stock and still achieve this. Choose -7 for the second stock since -7 is the smallest (absolute value) coefficient of 12 that allows us to get revenue of 1: 0*6 + -7*12 + 5*17 = 1.

  2. {10,4}

    Returns: { 1, -2 }

    The smallest positive revenue is 2. Both {1,-2} and {-1,3} achieve this. {1,-2} is chosen by the tie breaking rule that says to choose the positive amount in preference to the same negative amount. 1*10 + -2*4 = 2, the minimum revenue possible.

  3. {79}

    Returns: { 1 }

  4. {60,60,60,60,60,60,60}

    Returns: { 0, 0, 0, 0, 0, 0, 1 }

  5. {84,23,46}

    Returns: { -3, 1, 5 }

  6. {23,46,84}

    Returns: { 1, 5, -3 }

  7. {46,84,23}

    Returns: { 0, -3, 11 }

  8. {4,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}

    Returns: { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }

  9. {1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000}

    Returns: { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }

  10. {1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000}

    Returns: { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }

  11. {17,19,5,13,11,37,59}

    Returns: { 0, 0, 0, 0, 0, 8, -5 }

  12. {17,19,37,11,22,110}

    Returns: { 0, 0, 3, 0, 0, -1 }

  13. {17,19,37,110,22,11}

    Returns: { 0, 0, 3, 0, 0, -10 }

  14. {17,19,37,11,110,33}

    Returns: { 0, 0, 3, 0, -1, 0 }

  15. {1000000,499999}

    Returns: { -249999, 499999 }

  16. {17,19,11,22,110,37,73,91,52,13,98,4002,32,501,772,999,112,97,95}

    Returns: { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -47, 48 }

  17. {17,19,11,22,110,37,73,91,52,13,98,4002,32,501,772,999,112,100,95}

    Returns: { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, 7, -5 }

  18. {255255,170170,102102,72930,46410,39270,30030}

    Returns: { 1, 1, -2, 2, 1, 4, -19 }

  19. {510510, 340340, 204204, 145860, 92820, 78540, 60060}

    Returns: { 1, 1, -2, 2, 1, 4, -19 }

  20. {20,4,9,6}

    Returns: { 0, 1, 1, -2 }

  21. { 200000, 240000, 300000, 150000, 400000, 120000, 800000 }

    Returns: { 0, 0, 0, -1, 0, 8, -1 }

  22. { 6, 15, 35 }

    Returns: { 1, 2, -1 }

  23. { 5, 5, 5, 5, 5 }

    Returns: { 0, 0, 0, 0, 1 }

  24. { 6, 10, 15 }

    Returns: { 1, 1, -1 }


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: