Skip to content

Commit 4dff0e1

Browse files
authored
Merge pull request #221 from sir-gon/feature/angry-children
[Hacker Rank] Interview Preparation Kit: Greedy Algorithms: Max Min. …
2 parents 8a6be3c + a23abb1 commit 4dff0e1

File tree

4 files changed

+252
-0
lines changed

4 files changed

+252
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# [Greedy Algorithms: Max Min](https://www.hackerrank.com/challenges/angry-children)
2+
3+
- Difficulty: `#medium`
4+
- Category: `#ProblemSolvingBasic` `#greedyalgorithms`
5+
6+
You will be given a list of integers, `arr`, and a single integer `k`.
7+
You must create an array of length `k` from elements of `arr` such that
8+
its unfairness is minimized.
9+
Call that array `arr'`.
10+
Unfairness of an array is calculated as
11+
12+
$$
13+
\textsf{\textbf{max(arr')}} - \textsf{\textbf{min(arr')}}
14+
$$
15+
16+
Where:
17+
18+
- max denotes the largest integer in `arr'`.
19+
- min denotes the smallest integer in `arr'`.
20+
21+
## Example
22+
23+
`arr = [1, 4, 7, 2]`
24+
`k = 2`
25+
26+
Pick any two elements, say `arr' = [4, 7]`.
27+
28+
$ \textsf{\textbf{unfairness}}
29+
=
30+
\textsf{\textbf{max(4, 7)}}
31+
-
32+
\textsf{\textbf{min(4, 7)}}
33+
= 7 - 4 = 3
34+
$
35+
36+
Testing for all pairs, the solution [1, 2] provides the minimum unfairness.
37+
38+
**Note**: Integers in `arr` may not be unique.
39+
40+
## Function Description
41+
42+
Complete the maxMin function in the editor below.
43+
maxMin has the following parameter(s):
44+
45+
- `int k`: the number of elements to select
46+
- `int arr[n]`: an array of integers
47+
48+
## Returns
49+
50+
- int: the minimum possible unfairness
51+
52+
## Input Format
53+
54+
The first line contains an integer , the number of elements in array .
55+
The second line contains an integer .
56+
Each of the next lines contains an integer where .
57+
58+
## Constraints
59+
60+
- $ 2 \leq n \leq 10^5 $
61+
- $ 2 \leq k \leq n $
62+
- $ 0 \leq arr[i] \leq 10^9 $
63+
64+
## Sample Input 0
65+
66+
```text
67+
7
68+
3
69+
10
70+
100
71+
300
72+
200
73+
1000
74+
20
75+
30
76+
```
77+
78+
## Sample Output 0
79+
80+
```text
81+
20
82+
```
83+
84+
## Explanation 0
85+
86+
Here `k = 3`; selecting the `3` integers `10, 20,30`, unfairness equals
87+
88+
```text
89+
max(10,20,30) - min(10,20,30) = 30 - 10 = 20
90+
```
91+
92+
## Sample Input 1
93+
94+
```text
95+
10
96+
4
97+
1
98+
2
99+
3
100+
4
101+
10
102+
20
103+
30
104+
40
105+
100
106+
200
107+
```
108+
109+
## Sample Output 1
110+
111+
```text
112+
3
113+
```
114+
115+
## Explanation 1
116+
117+
Here `k = 4`; selecting the `4` integers `1, 2, 3, 4`, unfairness equals
118+
119+
```text
120+
max(1,2,3,4) - min(1,2,3,4) = 4 - 1 = 3
121+
```
122+
123+
## Sample Input 2
124+
125+
```text
126+
5
127+
2
128+
1
129+
2
130+
1
131+
2
132+
1
133+
```
134+
135+
## Sample Output 2
136+
137+
```text
138+
0
139+
```
140+
141+
## Explanation 2
142+
143+
Here `k = 2`. `arr' = [2, 2]` or `arr' = [1, 1]` give the minimum unfairness of `0`.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// @link Problem definition [[docs/hackerrank/interview_preparation_kit/greedy_algorithms/angry-children.md]]
2+
3+
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit.greedy_algorithms;
4+
5+
using System.Diagnostics.CodeAnalysis;
6+
7+
8+
/**
9+
* AngryFlorist.
10+
*
11+
*/
12+
public static class AngryFlorist
13+
{
14+
/**
15+
* maxMin.
16+
*/
17+
18+
public static int maxMin(int k, List<int> arr)
19+
{
20+
List<int> sortedlist = new List<int>(arr);
21+
sortedlist.Sort();
22+
int result = sortedlist[sortedlist.Count - 1] - sortedlist[0];
23+
24+
for (int i = 0; i < sortedlist.Count - k + 1; i++)
25+
{
26+
int tmin = sortedlist[i];
27+
int tmax = sortedlist[i + k - 1];
28+
result = Math.Min(result, tmax - tmin);
29+
}
30+
31+
return result;
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[
2+
{
3+
"title": "Sample Test case 0",
4+
"k": 3,
5+
"arr": [10, 100, 300, 200, 1000, 20, 30],
6+
"expected": 20
7+
},
8+
{
9+
"title": "Sample Test case 1",
10+
"k": 4,
11+
"arr": [1, 2, 3, 4, 10, 20, 30, 40, 100, 200],
12+
"expected": 3
13+
},
14+
{
15+
"title": "Sample Test case 2",
16+
"k": 2,
17+
"arr": [1, 2, 1, 2, 1],
18+
"expected": 0
19+
},
20+
{
21+
"title": "Sample Test case 16",
22+
"k": 3,
23+
"arr": [100, 200, 300, 350, 400, 401, 402],
24+
"expected": 2
25+
}
26+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
namespace algorithm_exercises_csharp_test.hackerrank.interview_preparation_kit.greedy_algorithms;
2+
3+
using algorithm_exercises_csharp_test.common;
4+
using algorithm_exercises_csharp.hackerrank.interview_preparation_kit.greedy_algorithms;
5+
6+
/**
7+
* AngryFloristTest.
8+
*/
9+
[TestClass]
10+
public class AngryFloristTest
11+
{
12+
public class AngryFloristTestCase(string title, int k, List<int> arr, int expected)
13+
{
14+
public string Title { get; } = title;
15+
public int K { get; } = k;
16+
public List<int> Arr { get; } = arr;
17+
public int Expected { get; } = expected;
18+
}
19+
20+
private List<AngryFloristTestCase> testCases { get; set; } = default!;
21+
22+
[TestInitialize]
23+
public void testInitialize()
24+
{
25+
testCases = JsonLoader.resourceLoad<List<AngryFloristTestCase>>(
26+
"hackerrank/interview_preparation_kit/greedy_algorithms/angry_children.testcases.json"
27+
) ?? [];
28+
}
29+
30+
[TestMethod]
31+
public void testLuckBalance()
32+
{
33+
foreach (AngryFloristTestCase test in testCases)
34+
{
35+
int result = AngryFlorist.maxMin(test.K, test.Arr);
36+
37+
Assert.AreEqual(
38+
test.Expected,
39+
result,
40+
string.Format(
41+
System.Globalization.CultureInfo.InvariantCulture,
42+
"AngryFlorist.maxMin({0}, {1}) => must be: {2}",
43+
test.K,
44+
test.Arr.ToString(),
45+
test.Expected
46+
)
47+
);
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)