Statistics

Problem Statement for "SquareMaking"

Problem Statement

You have four sticks. Their lengths are a, b, c, and d. There is also a stick altering store. For one coin they can change (i.e., either increase or decrease) the length of any stick by 1. You can use the store as many times as you want. You would like to build a square out of your four sticks. Therefore, you need four sticks that each have the same length. Note that the length must be positive.

You are given the ints a, b, c, and d. Compute and return the smallest number of coins you need to spend at the store.

Definition

Class:
SquareMaking
Method:
getMinimalPrice
Parameters:
int, int, int, int
Returns:
int
Method signature:
int getMinimalPrice(int a, int b, int c, int d)
(be sure your method is public)

Constraints

  • a will be between 1 and 1,000,000, inclusive.
  • b will be between 1 and 1,000,000, inclusive.
  • c will be between 1 and 1,000,000, inclusive.
  • d will be between 1 and 1,000,000, inclusive.

Examples

  1. 1

    1

    1

    1

    Returns: 0

  2. 8

    8

    1

    1

    Returns: 14

  3. 894

    197

    325

    232

    Returns: 790

  4. 333625

    453145

    800800

    907251

    Returns: 921281

  5. 412396

    690334

    841896

    843011

    Returns: 582177

  6. 74578

    199252

    959139

    470888

    Returns: 1156197

  7. 9859

    748096

    475634

    928248

    Returns: 1190851

  8. 125987

    288891

    590429

    264237

    Returns: 489096

  9. 102149

    813081

    209411

    219907

    Returns: 721428

  10. 106895

    882089

    329221

    325539

    Returns: 778876

  11. 703054

    376259

    887448

    910770

    Returns: 718905

  12. 332532

    235422

    924898

    492387

    Returns: 849331

  13. 624191

    691939

    710197

    300407

    Returns: 477538

  14. 196518

    359971

    30518

    274071

    Returns: 407006

  15. 278816

    989449

    521171

    146646

    Returns: 1085158

  16. 230070

    37311

    92074

    618927

    Returns: 719612

  17. 472596

    96820

    586155

    313494

    Returns: 648437

  18. 396918

    167704

    134778

    910077

    Returns: 1004513

  19. 764255

    683844

    914823

    513046

    Returns: 482188

  20. 629174

    695377

    232688

    426437

    Returns: 665426

  21. 524637

    679894

    508634

    280107

    Returns: 415790

  22. 942288

    73481

    513380

    796022

    Returns: 1151449

  23. 879446

    293934

    655030

    539062

    Returns: 701480

  24. 588704

    405440

    61481

    472140

    Returns: 593923

  25. 874739

    325809

    521569

    137548

    Returns: 932951

  26. 955287

    879484

    572289

    686393

    Returns: 576089

  27. 692742

    688417

    584765

    11045

    Returns: 785349

  28. 246946

    473295

    527185

    771841

    Returns: 578785

  29. 1000000

    1000000

    1000000

    1000000

    Returns: 0

  30. 1000000

    1

    1000000

    1000000

    Returns: 999999

  31. 1000000

    1

    1

    1000000

    Returns: 1999998

  32. 1000000

    1

    1

    1

    Returns: 999999

  33. 5

    6

    5

    5

    Returns: 1

    Here the optimal solution is to pay 1 coin to decrease b by 1. After the change you will have four equally long sticks.

  34. 4

    1

    5

    4

    Returns: 4

    Here the optimal side length is 4. Hence, you need to pay the store to increase b three times and to decrease c once. The total amount spent at the store will therefore be 4 coins.

  35. 2017

    2017

    2017

    2017

    Returns: 0

    Here you do not need to change sticks at all.

  36. 705451

    751563

    608515

    994713

    Returns: 432310

  37. 1

    1

    1000000

    1000000

    Returns: 1999998

  38. 1

    2

    3

    4

    Returns: 4

  39. 6

    7

    2

    1

    Returns: 10

  40. 2

    3

    3

    3

    Returns: 1

  41. 1

    1

    1

    10

    Returns: 9

  42. 1

    1

    5

    5

    Returns: 8

  43. 1

    2

    3

    10

    Returns: 10

  44. 1

    10

    10

    10000

    Returns: 9999

  45. 100

    100

    100

    1

    Returns: 99

  46. 1

    3

    4

    2

    Returns: 4

  47. 1

    10

    10

    10

    Returns: 9

  48. 1

    1

    1

    100

    Returns: 99

  49. 5

    5

    1

    5

    Returns: 4


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: