Skip to content

Commit 2a0a8cb

Browse files
authored
Merge pull request #339 from sir-gon/feature/angry-children
[Hacker Rank] Interview Preparation Kit: Greedy Algorithms: Max Min. …
2 parents 5085dac + fcffa4a commit 2a0a8cb

File tree

4 files changed

+253
-0
lines changed

4 files changed

+253
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package ae.hackerrank.interview_preparation_kit.greedy_algorithms;
2+
3+
import java.util.Collections;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
7+
/**
8+
* AngryFlorist.
9+
*
10+
* @link Problem definition
11+
* [[docs/hackerrank/interview_preparation_kit/greedy_algorithms/angry-children.md]]
12+
*/
13+
public class AngryFlorist {
14+
15+
private AngryFlorist() {
16+
}
17+
18+
/**
19+
* maxMin.
20+
*/
21+
public static int maxMin(int k, List<Integer> arr) {
22+
List<Integer> sortedNums = arr.stream().collect(Collectors.toList());
23+
Collections.sort(sortedNums);
24+
25+
int result = sortedNums.get(sortedNums.size() - 1) - sortedNums.get(0);
26+
27+
for (int i = 0; i < sortedNums.size() - k + 1; i++) {
28+
int tmin = sortedNums.get(i);
29+
int tmax = sortedNums.get(i + k - 1);
30+
result = Math.min(result, tmax - tmin);
31+
}
32+
33+
return result;
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package ae.hackerrank.interview_preparation_kit.greedy_algorithms;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.io.IOException;
6+
import java.util.List;
7+
import org.junit.jupiter.api.BeforeAll;
8+
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.TestInstance;
10+
import org.junit.jupiter.api.TestInstance.Lifecycle;
11+
import util.JsonLoader;
12+
13+
/**
14+
* AngryFloristTest.
15+
*/
16+
@TestInstance(Lifecycle.PER_CLASS)
17+
class AngryFloristTest {
18+
public static class AngryFloristTestCase {
19+
public String title;
20+
public Integer k;
21+
public List<Integer> arr;
22+
public Integer expected;
23+
}
24+
25+
private List<AngryFloristTestCase> testCases;
26+
27+
@BeforeAll
28+
void setup() throws IOException {
29+
String path = String.join("/",
30+
"hackerrank",
31+
"interview_preparation_kit",
32+
"greedy_algorithms",
33+
"angry_children.testcases.json");
34+
this.testCases = JsonLoader.loadJson(path, AngryFloristTestCase.class);
35+
}
36+
37+
@Test
38+
void testLuckBalance() {
39+
for (AngryFloristTestCase test : testCases) {
40+
Integer result = AngryFlorist.maxMin(test.k, test.arr);
41+
42+
assertEquals(test.expected, result,
43+
"%s(%s) => must be: %d".formatted(
44+
"AngryFlorist.maxMin",
45+
test.arr.toString(),
46+
test.expected));
47+
}
48+
}
49+
}
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+
]
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`.

0 commit comments

Comments
 (0)