Skip to content

Commit 9d5ef53

Browse files
authored
Merge pull request #23 from sir-gon/feature/mini_max_sum
[Hacker Rank]: Warmup: Mini-Max Sum solved ✅.
2 parents dab3b8a + 1468a29 commit 9d5ef53

File tree

5 files changed

+186
-0
lines changed

5 files changed

+186
-0
lines changed
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.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <string>
2+
#include <vector>
3+
4+
#pragma once
5+
6+
namespace hackerrank::warmup {
7+
std::string miniMaxSumCalculate(const std::vector<int> &arr);
8+
void miniMaxSum(const std::vector<int> &ar);
9+
} // namespace hackerrank::warmup
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <exercises/hackerrank/warmup/mini_max_sum.hpp>
2+
3+
/**
4+
* @link Problem definition [[docs/hackerrank/warmup/mini_max_sum.md]]
5+
*/
6+
7+
#include <algorithm>
8+
#include <iostream>
9+
#include <sstream>
10+
#include <stdexcept>
11+
#include <string>
12+
#include <vector>
13+
14+
namespace hackerrank::warmup {
15+
std::string miniMaxSumCalculate(const std::vector<int> &arr) {
16+
if (arr.size() < 2) {
17+
throw std::invalid_argument("List too short. Pass at least 2 elements.");
18+
}
19+
20+
long tsum = 0;
21+
long tmin = arr[0];
22+
long tmax = arr[1];
23+
24+
for (const int &value : arr) {
25+
auto cvalue = (long)value;
26+
tsum += cvalue;
27+
tmin = std::min(tmin, cvalue);
28+
tmax = std::max(tmax, cvalue);
29+
}
30+
31+
std::stringstream result;
32+
result << tsum - tmax << " " << tsum - tmin;
33+
34+
return result.str();
35+
}
36+
37+
void miniMaxSum(const std::vector<int> &arr) {
38+
std::cout << miniMaxSumCalculate(arr) << std::endl;
39+
}
40+
41+
} // namespace hackerrank::warmup
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <catch2/catch_test_macros.hpp>
2+
3+
#include <exercises/hackerrank/warmup/mini_max_sum.hpp>
4+
#include <iostream>
5+
#include <stdexcept>
6+
#include <string>
7+
#include <vector>
8+
9+
#include <filesystem>
10+
#include <fstream>
11+
#include <nlohmann/json.hpp>
12+
using json = nlohmann::json;
13+
14+
TEST_CASE("miniMaxSum JSON Test Cases", "[warmup]") {
15+
std::filesystem::path cwd = std::filesystem::current_path();
16+
std::string path =
17+
cwd.string() + "/unit/lib/hackerrank/warmup/mini_max_sum.testcases.json";
18+
19+
INFO("miniMaxSum JSON test cases FILE: " << path);
20+
21+
std::ifstream f(path);
22+
json data = json::parse(f);
23+
24+
for (auto testcase : data) {
25+
std::string result =
26+
hackerrank::warmup::miniMaxSumCalculate(testcase["input"]);
27+
hackerrank::warmup::miniMaxSum(testcase["input"]);
28+
29+
CHECK(result == testcase["expected"]);
30+
}
31+
}
32+
33+
TEST_CASE("miniMaxSum Edge Cases", "[warmup]") {
34+
std::vector<int> empty;
35+
CHECK_THROWS_AS(hackerrank::warmup::miniMaxSumCalculate(empty),
36+
std::invalid_argument);
37+
CHECK_THROWS_AS(hackerrank::warmup::miniMaxSum(empty), std::invalid_argument);
38+
CHECK_THROWS_AS(hackerrank::warmup::miniMaxSumCalculate({}),
39+
std::invalid_argument);
40+
CHECK_THROWS_AS(hackerrank::warmup::miniMaxSum({}), std::invalid_argument);
41+
CHECK_THROWS_AS(hackerrank::warmup::miniMaxSumCalculate({1}),
42+
std::invalid_argument);
43+
CHECK_THROWS_AS(hackerrank::warmup::miniMaxSum({1}), std::invalid_argument);
44+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[
2+
{
3+
"title": "test case 0",
4+
"input": [1, 2, 3, 4, 5],
5+
"expected": "10 14"
6+
},
7+
{
8+
"title": "",
9+
"input": [5, 4, 3, 2, 1],
10+
"expected": "10 14"
11+
},
12+
{
13+
"title": "test case 14",
14+
"input": [7, 69, 2, 221, 8974],
15+
"expected": "299 9271"
16+
},
17+
{
18+
"title": "test case 1",
19+
"input": [256741038, 623958417, 467905213, 714532089, 938071625],
20+
"expected": "2063136757 2744467344"
21+
}
22+
]

0 commit comments

Comments
 (0)