Skip to content

Commit 01f1759

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank]: Mini-Max Sum solved ✓
1 parent 57d8de4 commit 01f1759

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed

docs/hackerrank/warmup/miniMaxSum.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# [Mini-Max Sum](https://www.hackerrank.com/challenges/mini-max-sum)
2+
3+
Difficulty: #easy
4+
Category: #warmup
5+
6+
Given five positive integers, find the minimum and maximum values
7+
that can be calculated by summing exactly four of the five integers.
8+
Then print the respective minimum and maximum values as a single line
9+
of two space-separated long integers.
10+
11+
## Example
12+
13+
$ arr = [1, 3, 5, 7, 9] $
14+
The minimum sum is $ 1 + 3 + 5 + 7 = 16 $ and the maximum sum
15+
is $ 3 + 5 + 7 + 9 = 24 $. The function prints
16+
17+
```text
18+
16 24
19+
```
20+
21+
## Function Description
22+
23+
Complete the miniMaxSum function in the editor below.
24+
miniMaxSum has the following parameter(s):
25+
26+
- arr: an array of $ 5 $ integers
27+
28+
## Print
29+
30+
Print two space-separated integers on one line: the minimum sum and
31+
the maximum sum of 4 of 5 elements.
32+
33+
## Input Format
34+
35+
A single line of five space-separated integers.
36+
37+
## Constraints
38+
39+
$ 1 \leq arra[i] \leq 10^9 $
40+
41+
## Output Format
42+
43+
Print two space-separated long integers denoting the respective minimum
44+
and maximum values that can be calculated by summing exactly four of the
45+
five integers. (The output can be greater than a 32 bit integer.)
46+
47+
## Sample Input
48+
49+
```text
50+
1 2 3 4 5
51+
```
52+
53+
## Sample Output
54+
55+
```text
56+
10 14
57+
```
58+
59+
## Explanation
60+
61+
The numbers are $ 1, 2, 3, 4, $ and $ 5 $. Calculate the following sums using
62+
four of the five integers:
63+
64+
1. Sum everything except $ 1 $, the sum is $ 2 + 3 + 4 + 5 = 14 $.
65+
2. Sum everything except $ 2 $, the sum is $ 1 + 3 + 4 + 5 = 13 $.
66+
3. Sum everything except $ 3 $, the sum is $ 1 + 2 + 4 + 5 = 12 $.
67+
4. Sum everything except $ 4 $, the sum is $ 1 + 2 + 3 + 5 = 11 $.
68+
5. Sum everything except $ 5 $, the sum is $ 1 + 2 + 3 + 4 = 10 $.
69+
70+
**Hints**: Beware of integer overflow! Use 64-bit Integer.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/warmup/miniMaxSum.md]]
3+
*/
4+
5+
package hackerrank
6+
7+
import (
8+
"errors"
9+
"fmt"
10+
)
11+
12+
func MiniMaxSum(arr []int) (string, error) {
13+
if len(arr) == 0 {
14+
return "", errors.New("empty input")
15+
}
16+
17+
sum := 0
18+
min := arr[0]
19+
max := arr[1]
20+
21+
for i := 0; i < len(arr); i++ {
22+
num := arr[i]
23+
sum += num
24+
25+
if num < min {
26+
min = num
27+
}
28+
29+
if num > max {
30+
max = num
31+
}
32+
}
33+
34+
return fmt.Sprintf("%d %d", sum-max, sum-min), nil
35+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package hackerrank
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestMiniMaxSumBorderCase(t *testing.T) {
11+
expectedSolution := []int{1, 1}
12+
input := []int{}
13+
14+
testname := fmt.Sprintf("MiniMaxSum(%v) => %d \n", input, expectedSolution)
15+
t.Run(testname, func(t *testing.T) {
16+
17+
_, err := MiniMaxSum(input)
18+
assert.Error(t, err)
19+
})
20+
}
21+
22+
func TestMiniMaxSum(t *testing.T) {
23+
var tests = []struct {
24+
input []int
25+
want string
26+
}{
27+
{input: []int{1, 2, 3, 4, 5}, want: "10 14"},
28+
{input: []int{5, 4, 3, 2, 1}, want: "10 14"},
29+
}
30+
31+
for _, tt := range tests {
32+
33+
testname := fmt.Sprintf("MiniMaxSum(%v) => %s", tt.input, tt.want)
34+
t.Run(testname, func(t *testing.T) {
35+
ans, _ := MiniMaxSum(tt.input)
36+
assert.Equal(t, tt.want, ans)
37+
})
38+
}
39+
}

0 commit comments

Comments
 (0)