Skip to content

[Hacker Rank] Warm up: Mini-Max Sum solved ✓ #163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package ae.hackerrank;

import java.util.List;

/**
* Mini-Max Sum.
*
* @link Problem definition [[docs/hackerrank/warmup/mini_max_sum.md]]
*/
public class MiniMaxSum {

private MiniMaxSum() {}

static java.util.logging.Logger logger = util.CustomLogger.getLogger();

/**
* miniMaxSum.
*
* @throws IllegalArgumentException arr Must have elements
*/
public static String miniMaxSum(List<Integer> arr) throws IllegalArgumentException {
if (arr == null || arr.isEmpty()) {
throw new IllegalArgumentException("Parameter cannot be empty");
}

int tsum = 0;
int tmin = arr.get(0);
int tmax = arr.get(0);

for (int value : arr) {
tsum += value;

tmin = Math.min(tmin, value);
tmax = Math.max(tmax, value);
}

return String.format("%d %d", tsum - tmax, tsum - tmin);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package ae.hackerrank;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;

@TestInstance(Lifecycle.PER_CLASS)
class MiniMaxSumTest {

public class MiniMaxSumTestCase {
public List<Integer> input;
public String expected;

public MiniMaxSumTestCase(List<Integer> _input, String _expected) {
this.input = _input;
this.expected = _expected;
}
}

public List<MiniMaxSumTestCase> testCases;

@BeforeAll
public void setup() {
this.testCases = Arrays.asList(
new MiniMaxSumTestCase(Arrays.asList(1, 2, 3, 4, 5), "10 14"),
new MiniMaxSumTestCase(Arrays.asList(5, 4, 3, 2, 1), "10 14")
);
}

@Test
void testMiniMaxSum() {

for (MiniMaxSumTestCase testCase : this.testCases) {
String solutionFound = MiniMaxSum.miniMaxSum(testCase.input);

assertEquals(testCase.expected, solutionFound,
String.format(
"CompareTriplets.compareTriplets() answer must be: %s",
testCase.expected.toString()));
}
}

@Test
void testMiniMaxSumNullInput() {

IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
MiniMaxSum.miniMaxSum(null);
});

String expectedMessage = "Parameter cannot be empty";
String actualMessage = exception.getMessage();

assertTrue(actualMessage.contains(expectedMessage));
}

@Test
void testMiniMaxSumEmptyInput() {
List<Integer> input = Arrays.asList();

IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
MiniMaxSum.miniMaxSum(input);
});

String expectedMessage = "Parameter cannot be empty";
String actualMessage = exception.getMessage();

assertTrue(actualMessage.contains(expectedMessage));
}
}
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.
Loading