Skip to content

Commit 96a54ed

Browse files
authored
Merge pull request #353 from sir-gon/feature/angry-children
[Hacker Rank] Interview Preparation Kit: Greedy Algorithms: Max Min. …
2 parents 9f28dff + 80f3ee3 commit 96a54ed

File tree

4 files changed

+242
-0
lines changed

4 files changed

+242
-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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/greedy_algorithms/angry-children.md]]
3+
*/
4+
5+
package hackerrank
6+
7+
import (
8+
"slices"
9+
)
10+
11+
func maxMin(k int32, arr []int32) int32 {
12+
// Sort the array
13+
sortedlist := make([]int32, len(arr))
14+
copy(sortedlist, arr)
15+
slices.Sort(sortedlist)
16+
17+
var result = sortedlist[len(arr)-1] - sortedlist[0]
18+
19+
for i := range int32(len(arr)) - k + 1 {
20+
var tmin = sortedlist[i]
21+
var tmax = sortedlist[i+k-1]
22+
result = min(result, tmax-tmin)
23+
}
24+
25+
return result
26+
}
27+
28+
func MaxMin(k int32, arr []int32) int32 {
29+
return maxMin(k, arr)
30+
}
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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package hackerrank
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"gon.cl/algorithms/utils"
10+
)
11+
12+
type AngryChildrenTestCase struct {
13+
K int32 `json:"k"`
14+
Arr []int32 `json:"arr"`
15+
Expected int32 `json:"expected"`
16+
}
17+
18+
var AngryChildrenTestCases []AngryChildrenTestCase
19+
20+
// You can use testing.T, if you want to test the code without benchmarking
21+
func AngryChildrenSetupSuite(t testing.TB) {
22+
wd, _ := os.Getwd()
23+
filepath := wd + "/angry_children.testcases.json"
24+
t.Log("Setup test cases from JSON: ", filepath)
25+
26+
var _, err = utils.LoadJSON(filepath, &AngryChildrenTestCases)
27+
if err != nil {
28+
t.Log(err)
29+
}
30+
}
31+
32+
func TestAngryChildren(t *testing.T) {
33+
34+
AngryChildrenSetupSuite(t)
35+
36+
for _, tt := range AngryChildrenTestCases {
37+
testname := fmt.Sprintf("MaxMin(%d, %v) => %v \n", tt.K, tt.Arr, tt.Expected)
38+
t.Run(testname, func(t *testing.T) {
39+
ans := MaxMin(tt.K, tt.Arr)
40+
assert.Equal(t, tt.Expected, ans)
41+
})
42+
}
43+
}

0 commit comments

Comments
 (0)