Skip to content

Commit e10249c

Browse files
authored
Merge pull request #414 from sir-gon/feature/angry-children
[Hacker Rank] Interview Preparation Kit: Greedy Algorithms: Max Min. …
2 parents ea1bedb + 7403190 commit e10249c

File tree

4 files changed

+211
-0
lines changed

4 files changed

+211
-0
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
2+
# [Greedy Algorithms: Max Min](https://www.hackerrank.com/challenges/angry-children)
3+
4+
- Difficulty: `#medium`
5+
- Category: `#ProblemSolvingBasic` `#greedy`
6+
7+
You will be given a list of integers, `arr`, and a single integer `k`.
8+
You must create an array of length `k` from elements of `arr` such that
9+
its unfairness is minimized.
10+
Call that array `arr'`.
11+
Unfairness of an array is calculated as
12+
13+
$$
14+
\textsf{\textbf{max(arr')}} - \textsf{\textbf{min(arr')}}
15+
$$
16+
17+
Where:
18+
19+
- max denotes the largest integer in `arr'`.
20+
- min denotes the smallest integer in `arr'`.
21+
22+
## Example
23+
24+
`arr = [1, 4, 7, 2]`
25+
`k = 2`
26+
27+
Pick any two elements, say `arr' = [4, 7]`.
28+
29+
$ \textsf{\textbf{unfairness}}
30+
=
31+
\textsf{\textbf{max(4, 7)}}
32+
-
33+
\textsf{\textbf{min(4, 7)}}
34+
= 7 - 4 = 3
35+
$
36+
37+
Testing for all pairs, the solution [1, 2] provides the minimum unfairness.
38+
39+
**Note**: Integers in `arr` may not be unique.
40+
41+
## Function Description
42+
43+
Complete the maxMin function in the editor below.
44+
maxMin has the following parameter(s):
45+
46+
- `int k`: the number of elements to select
47+
- `int arr[n]`: an array of integers
48+
49+
## Returns
50+
51+
- int: the minimum possible unfairness
52+
53+
## Input Format
54+
55+
The first line contains an integer , the number of elements in array .
56+
The second line contains an integer .
57+
Each of the next lines contains an integer where .
58+
59+
## Constraints
60+
61+
- $ 2 \leq n \leq 10^5 $
62+
- $ 2 \leq k \leq n $
63+
- $ 0 \leq arr[i] \leq 10^9 $
64+
65+
## Sample Input 0
66+
67+
```text
68+
7
69+
3
70+
10
71+
100
72+
300
73+
200
74+
1000
75+
20
76+
30
77+
```
78+
79+
## Sample Output 0
80+
81+
```text
82+
20
83+
```
84+
85+
## Explanation 0
86+
87+
Here `k = 3`; selecting the `3` integers `10, 20,30`, unfairness equals
88+
89+
```text
90+
max(10,20,30) - min(10,20,30) = 30 - 10 = 20
91+
```
92+
93+
## Sample Input 1
94+
95+
```text
96+
10
97+
4
98+
1
99+
2
100+
3
101+
4
102+
10
103+
20
104+
30
105+
40
106+
100
107+
200
108+
```
109+
110+
## Sample Output 1
111+
112+
```text
113+
3
114+
```
115+
116+
## Explanation 1
117+
118+
Here `k = 4`; selecting the `4` integers `1, 2, 3, 4`, unfairness equals
119+
120+
```text
121+
max(1,2,3,4) - min(1,2,3,4) = 4 - 1 = 3
122+
```
123+
124+
## Sample Input 2
125+
126+
```text
127+
5
128+
2
129+
1
130+
2
131+
1
132+
2
133+
1
134+
```
135+
136+
## Sample Output 2
137+
138+
```text
139+
0
140+
```
141+
142+
## Explanation 2
143+
144+
Here `k = 2`. `arr' = [2, 2]` or `arr' = [1, 1]` give the minimum unfairness of `0`.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { describe, expect, it } from '@jest/globals';
2+
import { logger as console } from '../../../logger';
3+
4+
import { maxMin } from './angry_children';
5+
6+
import TEST_CASES from './angry_children.testcases.json';
7+
8+
describe('luck-balance', () => {
9+
it('luckBalance test cases', () => {
10+
expect.assertions(4);
11+
12+
TEST_CASES.forEach((test) => {
13+
const answer = maxMin(test.k, test.arr);
14+
15+
console.debug(
16+
`luckBalance(${test.k}, ${test.arr}) solution found: ${answer}`
17+
);
18+
19+
expect(answer).toStrictEqual(test.expected);
20+
});
21+
});
22+
});
Lines changed: 26 additions & 0 deletions
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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/greedy_algorithms/angry-children.md]]
3+
*/
4+
5+
export function maxMin(k: number, arr: number[]): number {
6+
// const copyArr: bigint[] = arr.map((x: number): bigint => BigInt(x));
7+
// const sortedlist = copyArr.sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
8+
const sortedlist = arr.sort((a: number, b: number) => a - b);
9+
10+
let result = sortedlist[sortedlist.length - 1] - sortedlist[0];
11+
12+
for (let i = 0; i < sortedlist.length - k + 1; i++) {
13+
const tmin = sortedlist[i];
14+
const tmax = sortedlist[i + k - 1];
15+
result = Math.min(result, tmax - tmin);
16+
}
17+
18+
return result;
19+
}

0 commit comments

Comments
 (0)