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
55 changes: 55 additions & 0 deletions docs/hackerrank/warmup/a_very_big_sum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# [A Very Big Sum](https://www.hackerrank.com/challenges/a-very-big-sum)

Difficulty: #easy
Category: #warmup

In this challenge, you are required to calculate and print the
sum of the elements in an array, keeping in mind that some of
those integers may be quite large.

## Function Description

Complete the aVeryBigSum function in the editor below.
It must return the sum of all array elements.

aVeryBigSum has the following parameter(s):

- int ar[n]: an array of integers.

## Return

- long: the sum of all array elements

## Input Format

The first line of the input consists of an integer n.
The next line contains space-separated integers contained in the array.

## Output Format

Return the integer sum of the elements in the array.

## Constraints

$ 1 <= n < 10 $ \
$ 0 <= ar[i] <= 10^10 $

## Sample Input

```text
5
1000000001 1000000002 1000000003 1000000004 1000000005
```

## Output

```text
5000000015
```

## Note

The range of the 32-bit integer is
($ -2^31 $) to ($ 2^31 - 1 $) or $ [-2147483648, 2147483647] $
When we add several integer values, the resulting sum might exceed the
above range. You might need to use long int C/C++/Java to store such sums.
6 changes: 6 additions & 0 deletions src/hackerrank/warmup/a_very_big_sum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @link Problem definition [[docs/hackerrank/warmup/a_very_big_sum.md]]

pub fn a_very_big_sum(ar: &[i64]) -> i64 {
ar.iter().sum()
}

1 change: 1 addition & 0 deletions src/hackerrank/warmup/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod solve_me_first;
pub mod simple_array_sum;
pub mod compare_triplets;
pub mod a_very_big_sum;
6 changes: 6 additions & 0 deletions tests/data/hackerrank/warmup/a_very_big_sum.testcases.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"input": [1000000001, 1000000002, 1000000003, 1000000004, 1000000005],
"expected": 5000000015
}
]
31 changes: 31 additions & 0 deletions tests/hackerrank/warmup/a_very_big_sum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use exercises::hackerrank::warmup::a_very_big_sum::a_very_big_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 AveryBigSumTestCase {
input: Vec<i64>,
expected: i64
}

static TEST_DATA: Lazy<Vec<AveryBigSumTestCase>> =
Lazy::new(|| load_json("tests/data/hackerrank/warmup/a_very_big_sum.testcases.json"));

#[test]
fn test_a_very_big_sum() {
println!("Testing hackerrank::warmup::a_very_big_sum::a_very_big_sum()");

for test_case in TEST_DATA.iter() {
let slice: &[i64] = &test_case.input;
let result = a_very_big_sum(slice);
assert_eq!(result, test_case.expected);
}
}
}
1 change: 1 addition & 0 deletions tests/hackerrank/warmup/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod solve_me_first;
pub mod simple_array_sum;
pub mod compare_triplets;
pub mod a_very_big_sum;
Loading