forked from keshavnandan/Topcoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWinterAndMandarins.txt
75 lines (45 loc) · 1.41 KB
/
WinterAndMandarins.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
PROBLEM STATEMENT
It's winter time!
Time to eat a lot of mandarins with your friends.
You have several bags with mandarins.
You are given an vector <int> bags.
For each i, the i-th element of bags represents the number of mandarins in the i-th bag.
You are also given an int K.
You want to choose exactly K bags and distribute them among you and your friends.
To be as fair as possible, you want to minimize the difference between the chosen bag with most mandarins and the chosen bag with fewest mandarins.
Return the smallest difference that can be achieved.
DEFINITION
Class:WinterAndMandarins
Method:getNumber
Parameters:vector <int>, int
Returns:int
Method signature:int getNumber(vector <int> bags, int K)
CONSTRAINTS
-bags will contain between 2 and 50 elements, inclusive.
-Each element of bags will be between 1 and 1,000,000,000, inclusive.
-K will be between 2 and N, inclusive, where N is the number of elements in bags.
EXAMPLES
0)
{10, 20, 30}
2
Returns: 10
There are three ways to choose two bags in this case: {10, 20}, {20, 30}, and {10, 30}.
Both in the first case and in the second case the difference between the largest and the smallest number of mandarins is 10.
In the third case the difference is 20.
Thus, the smallest possible difference is 10.
1)
{4, 7, 4}
3
Returns: 3
2)
{4, 1, 2, 3}
3
Returns: 2
3)
{5, 4, 6, 1, 9, 4, 2, 7, 5, 6}
4
Returns: 1
4)
{47, 1000000000, 1, 74}
2
Returns: 27