Skip to content

Commit afa0282

Browse files
authored
Merge pull request #163 from sir-gon/feature/mini_max_sum
[Hacker Rank] Warm up: Mini-Max Sum solved ✓
2 parents b57b0ea + 85c4d5b commit afa0282

File tree

3 files changed

+185
-0
lines changed

3 files changed

+185
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package ae.hackerrank;
2+
3+
import java.util.List;
4+
5+
/**
6+
* Mini-Max Sum.
7+
*
8+
* @link Problem definition [[docs/hackerrank/warmup/mini_max_sum.md]]
9+
*/
10+
public class MiniMaxSum {
11+
12+
private MiniMaxSum() {}
13+
14+
static java.util.logging.Logger logger = util.CustomLogger.getLogger();
15+
16+
/**
17+
* miniMaxSum.
18+
*
19+
* @throws IllegalArgumentException arr Must have elements
20+
*/
21+
public static String miniMaxSum(List<Integer> arr) throws IllegalArgumentException {
22+
if (arr == null || arr.isEmpty()) {
23+
throw new IllegalArgumentException("Parameter cannot be empty");
24+
}
25+
26+
int tsum = 0;
27+
int tmin = arr.get(0);
28+
int tmax = arr.get(0);
29+
30+
for (int value : arr) {
31+
tsum += value;
32+
33+
tmin = Math.min(tmin, value);
34+
tmax = Math.max(tmax, value);
35+
}
36+
37+
return String.format("%d %d", tsum - tmax, tsum - tmin);
38+
}
39+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package ae.hackerrank;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import java.util.Arrays;
8+
import java.util.List;
9+
import org.junit.jupiter.api.BeforeAll;
10+
import org.junit.jupiter.api.Test;
11+
import org.junit.jupiter.api.TestInstance;
12+
import org.junit.jupiter.api.TestInstance.Lifecycle;
13+
14+
@TestInstance(Lifecycle.PER_CLASS)
15+
class MiniMaxSumTest {
16+
17+
public class MiniMaxSumTestCase {
18+
public List<Integer> input;
19+
public String expected;
20+
21+
public MiniMaxSumTestCase(List<Integer> _input, String _expected) {
22+
this.input = _input;
23+
this.expected = _expected;
24+
}
25+
}
26+
27+
public List<MiniMaxSumTestCase> testCases;
28+
29+
@BeforeAll
30+
public void setup() {
31+
this.testCases = Arrays.asList(
32+
new MiniMaxSumTestCase(Arrays.asList(1, 2, 3, 4, 5), "10 14"),
33+
new MiniMaxSumTestCase(Arrays.asList(5, 4, 3, 2, 1), "10 14")
34+
);
35+
}
36+
37+
@Test
38+
void testMiniMaxSum() {
39+
40+
for (MiniMaxSumTestCase testCase : this.testCases) {
41+
String solutionFound = MiniMaxSum.miniMaxSum(testCase.input);
42+
43+
assertEquals(testCase.expected, solutionFound,
44+
String.format(
45+
"CompareTriplets.compareTriplets() answer must be: %s",
46+
testCase.expected.toString()));
47+
}
48+
}
49+
50+
@Test
51+
void testMiniMaxSumNullInput() {
52+
53+
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
54+
MiniMaxSum.miniMaxSum(null);
55+
});
56+
57+
String expectedMessage = "Parameter cannot be empty";
58+
String actualMessage = exception.getMessage();
59+
60+
assertTrue(actualMessage.contains(expectedMessage));
61+
}
62+
63+
@Test
64+
void testMiniMaxSumEmptyInput() {
65+
List<Integer> input = Arrays.asList();
66+
67+
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
68+
MiniMaxSum.miniMaxSum(input);
69+
});
70+
71+
String expectedMessage = "Parameter cannot be empty";
72+
String actualMessage = exception.getMessage();
73+
74+
assertTrue(actualMessage.contains(expectedMessage));
75+
}
76+
}
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)