Skip to content

Commit c15c753

Browse files
authored
Merge pull request #40 from sir-gon/feature/mini_max_sum
[Hacker Rank]: warmup: Mini-Max Sum solved ✓
2 parents 29990e9 + 3542e51 commit c15c753

File tree

6 files changed

+151
-0
lines changed

6 files changed

+151
-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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// @link Problem definition [[docs/hackerrank/warmup/mini_max_sum.md]]
2+
3+
pub fn mini_max_sum(arr: &[i32]) -> String {
4+
if arr.is_empty() {
5+
panic!("Empty input");
6+
}
7+
8+
let mut tsum = 0;
9+
let mut tmin: i32 = arr[0];
10+
let mut tmax: i32 = arr[1];
11+
12+
for value in arr.iter() {
13+
tsum += *value;
14+
15+
if *value < tmin {
16+
tmin = *value;
17+
}
18+
19+
if *value > tmax {
20+
tmax = *value;
21+
}
22+
}
23+
24+
let result = format!("{} {}", tsum - tmax, tsum - tmin);
25+
26+
result
27+
}
28+
29+
#[allow(non_snake_case)]
30+
pub fn miniMaxSum(arr: &[i32]) {
31+
print!("{}", mini_max_sum(arr))
32+
}

src/hackerrank/warmup/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ pub mod a_very_big_sum;
55
pub mod diagonal_difference;
66
pub mod plus_minus;
77
pub mod staircase;
8+
pub mod mini_max_sum;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[
2+
{"input": [1, 2, 3, 4, 5], "expected": "10 14"},
3+
{"input": [5, 4, 3, 2, 1], "expected": "10 14"}
4+
]
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use exercises::hackerrank::warmup::mini_max_sum::*;
2+
use once_cell::sync::Lazy;
3+
use serde::Deserialize;
4+
5+
use crate::common;
6+
use common::utils::load_json;
7+
8+
#[cfg(test)]
9+
mod tests {
10+
use super::*;
11+
12+
#[derive(Debug, Deserialize)]
13+
struct MiniMaxSumTest {
14+
input: Vec<i32>,
15+
expected: String
16+
}
17+
18+
static TEST_DATA: Lazy<Vec<MiniMaxSumTest>> =
19+
Lazy::new(|| load_json("tests/data/hackerrank/warmup/mini_max_sum.testcases.json"));
20+
21+
#[test]
22+
fn test_mini_max_sum() {
23+
println!("Testing hackerrank::warmup::mini_max_sum::mini_max_sum()");
24+
25+
for test_case in TEST_DATA.iter() {
26+
let slice: &[i32] = &test_case.input;
27+
let result = mini_max_sum(slice);
28+
miniMaxSum(slice);
29+
30+
assert_eq!(result, test_case.expected);
31+
}
32+
}
33+
34+
#[test]
35+
fn test_mini_max_sum_edge_case() {
36+
println!("Testing hackerrank::warmup::mini_max_sum::mini_max_sum()");
37+
38+
let input: Vec<i32> = vec![];
39+
let slice: &[i32] = &input;
40+
let result = std::panic::catch_unwind(|| mini_max_sum(slice));
41+
assert!(result.is_err());
42+
}
43+
}

tests/hackerrank/warmup/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ pub mod a_very_big_sum;
55
pub mod diagonal_difference;
66
pub mod plus_minus;
77
pub mod staircase;
8+
pub mod mini_max_sum;

0 commit comments

Comments
 (0)