Skip to content

[Hacker Rank]: Warmup: Mini-Max Sum solved ✅. #23

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
Sep 13, 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
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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <string>
#include <vector>

#pragma once

namespace hackerrank::warmup {
std::string miniMaxSumCalculate(const std::vector<int> &arr);
void miniMaxSum(const std::vector<int> &ar);
} // namespace hackerrank::warmup
41 changes: 41 additions & 0 deletions src/lib/exercises/src/hackerrank/warmup/mini_max_sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <exercises/hackerrank/warmup/mini_max_sum.hpp>

/**
* @link Problem definition [[docs/hackerrank/warmup/mini_max_sum.md]]
*/

#include <algorithm>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>

namespace hackerrank::warmup {
std::string miniMaxSumCalculate(const std::vector<int> &arr) {
if (arr.size() < 2) {
throw std::invalid_argument("List too short. Pass at least 2 elements.");
}

long tsum = 0;
long tmin = arr[0];
long tmax = arr[1];

for (const int &value : arr) {
auto cvalue = (long)value;
tsum += cvalue;
tmin = std::min(tmin, cvalue);
tmax = std::max(tmax, cvalue);
}

std::stringstream result;
result << tsum - tmax << " " << tsum - tmin;

return result.str();
}

void miniMaxSum(const std::vector<int> &arr) {
std::cout << miniMaxSumCalculate(arr) << std::endl;
}

} // namespace hackerrank::warmup
44 changes: 44 additions & 0 deletions src/tests/unit/lib/hackerrank/warmup/mini_max_sum.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <catch2/catch_test_macros.hpp>

#include <exercises/hackerrank/warmup/mini_max_sum.hpp>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>

#include <filesystem>
#include <fstream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;

TEST_CASE("miniMaxSum JSON Test Cases", "[warmup]") {
std::filesystem::path cwd = std::filesystem::current_path();
std::string path =
cwd.string() + "/unit/lib/hackerrank/warmup/mini_max_sum.testcases.json";

INFO("miniMaxSum JSON test cases FILE: " << path);

std::ifstream f(path);
json data = json::parse(f);

for (auto testcase : data) {
std::string result =
hackerrank::warmup::miniMaxSumCalculate(testcase["input"]);
hackerrank::warmup::miniMaxSum(testcase["input"]);

CHECK(result == testcase["expected"]);
}
}

TEST_CASE("miniMaxSum Edge Cases", "[warmup]") {
std::vector<int> empty;
CHECK_THROWS_AS(hackerrank::warmup::miniMaxSumCalculate(empty),
std::invalid_argument);
CHECK_THROWS_AS(hackerrank::warmup::miniMaxSum(empty), std::invalid_argument);
CHECK_THROWS_AS(hackerrank::warmup::miniMaxSumCalculate({}),
std::invalid_argument);
CHECK_THROWS_AS(hackerrank::warmup::miniMaxSum({}), std::invalid_argument);
CHECK_THROWS_AS(hackerrank::warmup::miniMaxSumCalculate({1}),
std::invalid_argument);
CHECK_THROWS_AS(hackerrank::warmup::miniMaxSum({1}), std::invalid_argument);
}
22 changes: 22 additions & 0 deletions src/tests/unit/lib/hackerrank/warmup/mini_max_sum.testcases.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"title": "test case 0",
"input": [1, 2, 3, 4, 5],
"expected": "10 14"
},
{
"title": "",
"input": [5, 4, 3, 2, 1],
"expected": "10 14"
},
{
"title": "test case 14",
"input": [7, 69, 2, 221, 8974],
"expected": "299 9271"
},
{
"title": "test case 1",
"input": [256741038, 623958417, 467905213, 714532089, 938071625],
"expected": "2063136757 2744467344"
}
]