Skip to content

Commit 3e025fe

Browse files
authored
Merge pull request #24 from sir-gon/feature/a-very-big-sum
[Hacker Rank]: A Very Big Sum solved ✓
2 parents decb92e + d48e28c commit 3e025fe

File tree

6 files changed

+100
-0
lines changed

6 files changed

+100
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# [A Very Big Sum](https://www.hackerrank.com/challenges/a-very-big-sum)
2+
3+
Difficulty: #easy
4+
Category: #warmup
5+
6+
In this challenge, you are required to calculate and print the
7+
sum of the elements in an array, keeping in mind that some of
8+
those integers may be quite large.
9+
10+
## Function Description
11+
12+
Complete the aVeryBigSum function in the editor below.
13+
It must return the sum of all array elements.
14+
15+
aVeryBigSum has the following parameter(s):
16+
17+
- int ar[n]: an array of integers.
18+
19+
## Return
20+
21+
- long: the sum of all array elements
22+
23+
## Input Format
24+
25+
The first line of the input consists of an integer n.
26+
The next line contains space-separated integers contained in the array.
27+
28+
## Output Format
29+
30+
Return the integer sum of the elements in the array.
31+
32+
## Constraints
33+
34+
$ 1 <= n < 10 $ \
35+
$ 0 <= ar[i] <= 10^10 $
36+
37+
## Sample Input
38+
39+
```text
40+
5
41+
1000000001 1000000002 1000000003 1000000004 1000000005
42+
```
43+
44+
## Output
45+
46+
```text
47+
5000000015
48+
```
49+
50+
## Note
51+
52+
The range of the 32-bit integer is
53+
($ -2^31 $) to ($ 2^31 - 1 $) or $ [-2147483648, 2147483647] $
54+
When we add several integer values, the resulting sum might exceed the
55+
above range. You might need to use long int C/C++/Java to store such sums.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @link Problem definition [[docs/hackerrank/warmup/a_very_big_sum.md]]
2+
3+
pub fn a_very_big_sum(ar: &[i64]) -> i64 {
4+
ar.iter().sum()
5+
}
6+

src/hackerrank/warmup/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pub mod solve_me_first;
22
pub mod simple_array_sum;
33
pub mod compare_triplets;
4+
pub mod a_very_big_sum;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
{
3+
"input": [1000000001, 1000000002, 1000000003, 1000000004, 1000000005],
4+
"expected": 5000000015
5+
}
6+
]
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use exercises::hackerrank::warmup::a_very_big_sum::a_very_big_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 AveryBigSumTestCase {
14+
input: Vec<i64>,
15+
expected: i64
16+
}
17+
18+
static TEST_DATA: Lazy<Vec<AveryBigSumTestCase>> =
19+
Lazy::new(|| load_json("tests/data/hackerrank/warmup/a_very_big_sum.testcases.json"));
20+
21+
#[test]
22+
fn test_a_very_big_sum() {
23+
println!("Testing hackerrank::warmup::a_very_big_sum::a_very_big_sum()");
24+
25+
for test_case in TEST_DATA.iter() {
26+
let slice: &[i64] = &test_case.input;
27+
let result = a_very_big_sum(slice);
28+
assert_eq!(result, test_case.expected);
29+
}
30+
}
31+
}

tests/hackerrank/warmup/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pub mod solve_me_first;
22
pub mod simple_array_sum;
33
pub mod compare_triplets;
4+
pub mod a_very_big_sum;

0 commit comments

Comments
 (0)