From 1468a2937a5b6ce0f14b6dbf8ebf4bbd7b94e65a Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Thu, 12 Sep 2024 13:59:51 -0300 Subject: [PATCH] =?UTF-8?q?[Hacker=20Rank]:=20Warmup:=20Mini-Max=20Sum=20s?= =?UTF-8?q?olved=20=E2=9C=85.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/hackerrank/warmup/mini_max_sum.md | 70 +++++++++++++++++++ .../hackerrank/warmup/mini_max_sum.hpp | 9 +++ .../src/hackerrank/warmup/mini_max_sum.cpp | 41 +++++++++++ .../hackerrank/warmup/mini_max_sum.test.cpp | 44 ++++++++++++ .../warmup/mini_max_sum.testcases.json | 22 ++++++ 5 files changed, 186 insertions(+) create mode 100644 docs/hackerrank/warmup/mini_max_sum.md create mode 100644 src/lib/exercises/include/exercises/hackerrank/warmup/mini_max_sum.hpp create mode 100644 src/lib/exercises/src/hackerrank/warmup/mini_max_sum.cpp create mode 100644 src/tests/unit/lib/hackerrank/warmup/mini_max_sum.test.cpp create mode 100644 src/tests/unit/lib/hackerrank/warmup/mini_max_sum.testcases.json diff --git a/docs/hackerrank/warmup/mini_max_sum.md b/docs/hackerrank/warmup/mini_max_sum.md new file mode 100644 index 0000000..d9ff3c8 --- /dev/null +++ b/docs/hackerrank/warmup/mini_max_sum.md @@ -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. diff --git a/src/lib/exercises/include/exercises/hackerrank/warmup/mini_max_sum.hpp b/src/lib/exercises/include/exercises/hackerrank/warmup/mini_max_sum.hpp new file mode 100644 index 0000000..94b890a --- /dev/null +++ b/src/lib/exercises/include/exercises/hackerrank/warmup/mini_max_sum.hpp @@ -0,0 +1,9 @@ +#include +#include + +#pragma once + +namespace hackerrank::warmup { +std::string miniMaxSumCalculate(const std::vector &arr); +void miniMaxSum(const std::vector &ar); +} // namespace hackerrank::warmup diff --git a/src/lib/exercises/src/hackerrank/warmup/mini_max_sum.cpp b/src/lib/exercises/src/hackerrank/warmup/mini_max_sum.cpp new file mode 100644 index 0000000..d9d946c --- /dev/null +++ b/src/lib/exercises/src/hackerrank/warmup/mini_max_sum.cpp @@ -0,0 +1,41 @@ +#include + +/** + * @link Problem definition [[docs/hackerrank/warmup/mini_max_sum.md]] + */ + +#include +#include +#include +#include +#include +#include + +namespace hackerrank::warmup { +std::string miniMaxSumCalculate(const std::vector &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 &arr) { + std::cout << miniMaxSumCalculate(arr) << std::endl; +} + +} // namespace hackerrank::warmup diff --git a/src/tests/unit/lib/hackerrank/warmup/mini_max_sum.test.cpp b/src/tests/unit/lib/hackerrank/warmup/mini_max_sum.test.cpp new file mode 100644 index 0000000..4e7ac2e --- /dev/null +++ b/src/tests/unit/lib/hackerrank/warmup/mini_max_sum.test.cpp @@ -0,0 +1,44 @@ +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +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 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); +} diff --git a/src/tests/unit/lib/hackerrank/warmup/mini_max_sum.testcases.json b/src/tests/unit/lib/hackerrank/warmup/mini_max_sum.testcases.json new file mode 100644 index 0000000..e4e3f8e --- /dev/null +++ b/src/tests/unit/lib/hackerrank/warmup/mini_max_sum.testcases.json @@ -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" + } +]