Skip to content

Commit efac2cd

Browse files
authored
Merge pull request #46 from sir-gon/feature/mini_max_sum
[Hacker Rank]: Warmup: Mini-Max Sum solved ✓
2 parents 84c360a + 927a8ac commit efac2cd

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace algorithm_exercises_csharp.hackerrank;
2+
3+
[TestClass]
4+
public class MiniMaxSumTest
5+
{
6+
public class MiniMaxSumTestCase
7+
{
8+
public List<int> input = [];
9+
public string expected = "";
10+
}
11+
12+
private static readonly MiniMaxSumTestCase[] tests = [
13+
new() { input = [1, 2, 3, 4, 5], expected = "10 14" },
14+
new() { input = [5, 4, 3, 2, 1], expected = "10 14" }
15+
];
16+
17+
[TestMethod]
18+
public void testMiniMaxSum()
19+
{
20+
string? result;
21+
22+
foreach (MiniMaxSumTestCase test in tests)
23+
{
24+
result = MiniMaxSum.miniMaxSum(test.input);
25+
Assert.AreEqual(test.expected, result);
26+
}
27+
}
28+
}
29+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// @link Problem definition [[docs/hackerrank/warmup/solve_me_first.md]]
2+
3+
namespace algorithm_exercises_csharp.hackerrank;
4+
5+
using System.Diagnostics.CodeAnalysis;
6+
using System.Text;
7+
8+
public class MiniMaxSum
9+
{
10+
[ExcludeFromCodeCoverage]
11+
protected MiniMaxSum() { }
12+
13+
public static string miniMaxSum(List<int> arr)
14+
{
15+
if (arr.Count == 0)
16+
{
17+
throw new ArgumentException("Parameter cannot be empty", nameof(arr));
18+
}
19+
20+
int tsum = 0;
21+
int tmin = arr[0];
22+
int tmax = arr[0];
23+
24+
foreach (int value in arr)
25+
{
26+
tsum += value;
27+
28+
tmin = Math.Min(tmin, value);
29+
tmax = Math.Max(tmax, value);
30+
}
31+
32+
return string.Format("{0} {1}", tsum - tmax, tsum - tmin);
33+
}
34+
35+
}
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.

0 commit comments

Comments
 (0)