Skip to content
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.
32 changes: 32 additions & 0 deletions src/hackerrank/warmup/mini_max_sum.rs
Original file line number Diff line number Diff line change
@@ -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))
}
1 change: 1 addition & 0 deletions src/hackerrank/warmup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
4 changes: 4 additions & 0 deletions tests/data/hackerrank/warmup/mini_max_sum.testcases.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
{"input": [1, 2, 3, 4, 5], "expected": "10 14"},
{"input": [5, 4, 3, 2, 1], "expected": "10 14"}
]
43 changes: 43 additions & 0 deletions tests/hackerrank/warmup/mini_max_sum.rs
Original file line number Diff line number Diff line change
@@ -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<i32>,
expected: String
}

static TEST_DATA: Lazy<Vec<MiniMaxSumTest>> =
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<i32> = vec![];
let slice: &[i32] = &input;
let result = std::panic::catch_unwind(|| mini_max_sum(slice));
assert!(result.is_err());
}
}
1 change: 1 addition & 0 deletions tests/hackerrank/warmup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Loading