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
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
Returns: 0
8
8
1
1
Returns: 14
894
197
325
232
Returns: 790
333625
453145
800800
907251
Returns: 921281
412396
690334
841896
843011
Returns: 582177
74578
199252
959139
470888
Returns: 1156197
9859
748096
475634
928248
Returns: 1190851
125987
288891
590429
264237
Returns: 489096
102149
813081
209411
219907
Returns: 721428
106895
882089
329221
325539
Returns: 778876
703054
376259
887448
910770
Returns: 718905
332532
235422
924898
492387
Returns: 849331
624191
691939
710197
300407
Returns: 477538
196518
359971
30518
274071
Returns: 407006
278816
989449
521171
146646
Returns: 1085158
230070
37311
92074
618927
Returns: 719612
472596
96820
586155
313494
Returns: 648437
396918
167704
134778
910077
Returns: 1004513
764255
683844
914823
513046
Returns: 482188
629174
695377
232688
426437
Returns: 665426
524637
679894
508634
280107
Returns: 415790
942288
73481
513380
796022
Returns: 1151449
879446
293934
655030
539062
Returns: 701480
588704
405440
61481
472140
Returns: 593923
874739
325809
521569
137548
Returns: 932951
955287
879484
572289
686393
Returns: 576089
692742
688417
584765
11045
Returns: 785349
246946
473295
527185
771841
Returns: 578785
1000000
1000000
1000000
1000000
Returns: 0
1000000
1
1000000
1000000
Returns: 999999
1000000
1
1
1000000
Returns: 1999998
1000000
1
1
1
Returns: 999999
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.
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.
2017
2017
2017
2017
Returns: 0
Here you do not need to change sticks at all.
705451
751563
608515
994713
Returns: 432310
1
1
1000000
1000000
Returns: 1999998
1
2
3
4
Returns: 4
6
7
2
1
Returns: 10
2
3
3
3
Returns: 1
1
1
1
10
Returns: 9
1
1
5
5
Returns: 8
1
2
3
10
Returns: 10
1
10
10
10000
Returns: 9999
100
100
100
1
Returns: 99
1
3
4
2
Returns: 4
1
10
10
10
Returns: 9
1
1
1
100
Returns: 99
5
5
1
5
Returns: 4