Skip to content

Commit

Permalink
Merge pull request #46 from sir-gon/feature/mini_max_sum
Browse files Browse the repository at this point in the history
[Hacker Rank]: Warmup: Mini-Max Sum solved ✓
  • Loading branch information
sir-gon authored May 22, 2024
2 parents 84c360a + 927a8ac commit efac2cd
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
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 algorithm-exercises-csharp/src/hackerrank/warmup/MiniMaxSum.cs
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);
}

}
70 changes: 70 additions & 0 deletions docs/hackerrank/warmup/mini_max_sum.md
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

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.

0 comments on commit efac2cd

Please sign in to comment.