From 3542e516beb31adc1f2a586cac4deb662cc685c2 Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Wed, 22 Oct 2025 11:24:19 -0300 Subject: [PATCH] =?UTF-8?q?[Hacker=20Rank]:=20warmup:=20Mini-Max=20Sum=20s?= =?UTF-8?q?olved=20=E2=9C=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/hackerrank/warmup/mini_max_sum.md | 70 +++++++++++++++++++ src/hackerrank/warmup/mini_max_sum.rs | 32 +++++++++ src/hackerrank/warmup/mod.rs | 1 + .../warmup/mini_max_sum.testcases.json | 4 ++ tests/hackerrank/warmup/mini_max_sum.rs | 43 ++++++++++++ tests/hackerrank/warmup/mod.rs | 1 + 6 files changed, 151 insertions(+) create mode 100644 docs/hackerrank/warmup/mini_max_sum.md create mode 100644 src/hackerrank/warmup/mini_max_sum.rs create mode 100644 tests/data/hackerrank/warmup/mini_max_sum.testcases.json create mode 100644 tests/hackerrank/warmup/mini_max_sum.rs 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/hackerrank/warmup/mini_max_sum.rs b/src/hackerrank/warmup/mini_max_sum.rs new file mode 100644 index 0000000..95cdbbb --- /dev/null +++ b/src/hackerrank/warmup/mini_max_sum.rs @@ -0,0 +1,32 @@ +// @link Problem definition [[docs/hackerrank/warmup/mini_max_sum.md]] + +pub fn mini_max_sum(arr: &[i32]) -> String { + if arr.is_empty() { + panic!("Empty input"); + } + + let mut tsum = 0; + let mut tmin: i32 = arr[0]; + let mut tmax: i32 = arr[1]; + + for value in arr.iter() { + tsum += *value; + + if *value < tmin { + tmin = *value; + } + + if *value > tmax { + tmax = *value; + } + } + + let result = format!("{} {}", tsum - tmax, tsum - tmin); + + result +} + +#[allow(non_snake_case)] +pub fn miniMaxSum(arr: &[i32]) { + print!("{}", mini_max_sum(arr)) +} diff --git a/src/hackerrank/warmup/mod.rs b/src/hackerrank/warmup/mod.rs index 1c43123..7efd7b4 100644 --- a/src/hackerrank/warmup/mod.rs +++ b/src/hackerrank/warmup/mod.rs @@ -5,3 +5,4 @@ pub mod a_very_big_sum; pub mod diagonal_difference; pub mod plus_minus; pub mod staircase; +pub mod mini_max_sum; diff --git a/tests/data/hackerrank/warmup/mini_max_sum.testcases.json b/tests/data/hackerrank/warmup/mini_max_sum.testcases.json new file mode 100644 index 0000000..b2d8c06 --- /dev/null +++ b/tests/data/hackerrank/warmup/mini_max_sum.testcases.json @@ -0,0 +1,4 @@ +[ + {"input": [1, 2, 3, 4, 5], "expected": "10 14"}, + {"input": [5, 4, 3, 2, 1], "expected": "10 14"} +] diff --git a/tests/hackerrank/warmup/mini_max_sum.rs b/tests/hackerrank/warmup/mini_max_sum.rs new file mode 100644 index 0000000..6bf2f82 --- /dev/null +++ b/tests/hackerrank/warmup/mini_max_sum.rs @@ -0,0 +1,43 @@ +use exercises::hackerrank::warmup::mini_max_sum::*; +use once_cell::sync::Lazy; +use serde::Deserialize; + +use crate::common; +use common::utils::load_json; + +#[cfg(test)] +mod tests { + use super::*; + + #[derive(Debug, Deserialize)] + struct MiniMaxSumTest { + input: Vec, + expected: String + } + + static TEST_DATA: Lazy> = + Lazy::new(|| load_json("tests/data/hackerrank/warmup/mini_max_sum.testcases.json")); + + #[test] + fn test_mini_max_sum() { + println!("Testing hackerrank::warmup::mini_max_sum::mini_max_sum()"); + + for test_case in TEST_DATA.iter() { + let slice: &[i32] = &test_case.input; + let result = mini_max_sum(slice); + miniMaxSum(slice); + + assert_eq!(result, test_case.expected); + } + } + + #[test] + fn test_mini_max_sum_edge_case() { + println!("Testing hackerrank::warmup::mini_max_sum::mini_max_sum()"); + + let input: Vec = vec![]; + let slice: &[i32] = &input; + let result = std::panic::catch_unwind(|| mini_max_sum(slice)); + assert!(result.is_err()); + } +} diff --git a/tests/hackerrank/warmup/mod.rs b/tests/hackerrank/warmup/mod.rs index 1c43123..7efd7b4 100644 --- a/tests/hackerrank/warmup/mod.rs +++ b/tests/hackerrank/warmup/mod.rs @@ -5,3 +5,4 @@ pub mod a_very_big_sum; pub mod diagonal_difference; pub mod plus_minus; pub mod staircase; +pub mod mini_max_sum;