-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from sir-gon/feature/mini_max_sum
[Hacker Rank]: Warmup: Mini-Max Sum solved ✓
- Loading branch information
Showing
3 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
algorithm-exercises-csharp-test/src/hackerrank/warmup/MiniMaxSum.Test.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
namespace algorithm_exercises_csharp.hackerrank; | ||
|
||
[TestClass] | ||
public class MiniMaxSumTest | ||
{ | ||
public class MiniMaxSumTestCase | ||
{ | ||
public List<int> input = []; | ||
public string expected = ""; | ||
} | ||
|
||
private static readonly MiniMaxSumTestCase[] tests = [ | ||
new() { input = [1, 2, 3, 4, 5], expected = "10 14" }, | ||
new() { input = [5, 4, 3, 2, 1], expected = "10 14" } | ||
]; | ||
|
||
[TestMethod] | ||
public void testMiniMaxSum() | ||
{ | ||
string? result; | ||
|
||
foreach (MiniMaxSumTestCase test in tests) | ||
{ | ||
result = MiniMaxSum.miniMaxSum(test.input); | ||
Assert.AreEqual(test.expected, result); | ||
} | ||
} | ||
} | ||
|
35 changes: 35 additions & 0 deletions
35
algorithm-exercises-csharp/src/hackerrank/warmup/MiniMaxSum.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// @link Problem definition [[docs/hackerrank/warmup/solve_me_first.md]] | ||
|
||
namespace algorithm_exercises_csharp.hackerrank; | ||
|
||
using System.Diagnostics.CodeAnalysis; | ||
using System.Text; | ||
|
||
public class MiniMaxSum | ||
{ | ||
[ExcludeFromCodeCoverage] | ||
protected MiniMaxSum() { } | ||
|
||
public static string miniMaxSum(List<int> arr) | ||
{ | ||
if (arr.Count == 0) | ||
{ | ||
throw new ArgumentException("Parameter cannot be empty", nameof(arr)); | ||
} | ||
|
||
int tsum = 0; | ||
int tmin = arr[0]; | ||
int tmax = arr[0]; | ||
|
||
foreach (int value in arr) | ||
{ | ||
tsum += value; | ||
|
||
tmin = Math.Min(tmin, value); | ||
tmax = Math.Max(tmax, value); | ||
} | ||
|
||
return string.Format("{0} {1}", tsum - tmax, tsum - tmin); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# [Mini-Max Sum](https://www.hackerrank.com/challenges/mini-max-sum) | ||
|
||
Difficulty: #easy | ||
Category: #warmup | ||
|
||
Given five positive integers, find the minimum and maximum values | ||
that can be calculated by summing exactly four of the five integers. | ||
Then print the respective minimum and maximum values as a single line | ||
of two space-separated long integers. | ||
|
||
## Example | ||
|
||
$ arr = [1, 3, 5, 7, 9] $ | ||
The minimum sum is $ 1 + 3 + 5 + 7 = 16 $ and the maximum sum | ||
is $ 3 + 5 + 7 + 9 = 24 $. The function prints | ||
|
||
```text | ||
16 24 | ||
``` | ||
|
||
## Function Description | ||
|
||
Complete the miniMaxSum function in the editor below. | ||
miniMaxSum has the following parameter(s): | ||
|
||
- arr: an array of $ 5 $ integers | ||
|
||
|
||
Print two space-separated integers on one line: the minimum sum and | ||
the maximum sum of 4 of 5 elements. | ||
|
||
## Input Format | ||
|
||
A single line of five space-separated integers. | ||
|
||
## Constraints | ||
|
||
$ 1 \leq arra[i] \leq 10^9 $ | ||
|
||
## Output Format | ||
|
||
Print two space-separated long integers denoting the respective minimum | ||
and maximum values that can be calculated by summing exactly four of the | ||
five integers. (The output can be greater than a 32 bit integer.) | ||
|
||
## Sample Input | ||
|
||
```text | ||
1 2 3 4 5 | ||
``` | ||
|
||
## Sample Output | ||
|
||
```text | ||
10 14 | ||
``` | ||
|
||
## Explanation | ||
|
||
The numbers are $ 1, 2, 3, 4, $ and $ 5 $. Calculate the following sums using | ||
four of the five integers: | ||
|
||
1. Sum everything except $ 1 $, the sum is $ 2 + 3 + 4 + 5 = 14 $. | ||
2. Sum everything except $ 2 $, the sum is $ 1 + 3 + 4 + 5 = 13 $. | ||
3. Sum everything except $ 3 $, the sum is $ 1 + 2 + 4 + 5 = 12 $. | ||
4. Sum everything except $ 4 $, the sum is $ 1 + 2 + 3 + 5 = 11 $. | ||
5. Sum everything except $ 5 $, the sum is $ 1 + 2 + 3 + 4 = 10 $. | ||
|
||
**Hints**: Beware of integer overflow! Use 64-bit Integer. |