Skip to content

Commit d7a451e

Browse files
authored
Merge pull request #6 from sir-gon/feature/a_very_big_sum
[Hacker Rank]: Warmup: A Very Big Sum. Solved ✅.
2 parents bf5a5d0 + 66749b1 commit d7a451e

File tree

5 files changed

+119
-0
lines changed

5 files changed

+119
-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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
3+
#ifdef __cplusplus
4+
extern "C" {
5+
#endif
6+
7+
long HACKERRANK_WARMUP_aVeryBigSum(int ar_count, const long *ar);
8+
9+
#ifdef __cplusplus
10+
} // extern "C"
11+
#endif
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <exercises/hackerrank/warmup/a_very_big_sum.h>
2+
3+
/**
4+
* @link Problem definition [[docs/hackerrank/warmup/a_very_big_sum.md]]
5+
*/
6+
7+
long HACKERRANK_WARMUP_aVeryBigSum(int ar_count, const long *ar) {
8+
long total = 0;
9+
10+
for (int i = 0; i < ar_count; i++) {
11+
total += ar[i];
12+
}
13+
14+
return total;
15+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <catch2/catch_test_macros.hpp>
2+
3+
#include <exercises/hackerrank/warmup/a_very_big_sum.h>
4+
#include <iostream>
5+
#include <vector>
6+
7+
#include <filesystem>
8+
#include <fstream>
9+
#include <nlohmann/json.hpp>
10+
using json = nlohmann::json;
11+
12+
TEST_CASE("aVeryBigSum JSON Test Cases",
13+
"[hackerrank] [jsontestcase] [warmup]") {
14+
std::filesystem::path cwd = std::filesystem::current_path();
15+
std::string path =
16+
cwd.string() +
17+
"/unit/lib/hackerrank/warmup/a_very_big_sum.testcases.json";
18+
19+
INFO("aVeryBigSum JSON test cases FILE: " << path);
20+
21+
std::ifstream f(path);
22+
json data = json::parse(f);
23+
24+
for (auto testcase : data) {
25+
auto size = static_cast<int>(testcase["input"].size());
26+
std::vector<long> input_vector = testcase["input"];
27+
const long *input_array = input_vector.data();
28+
29+
long result = HACKERRANK_WARMUP_aVeryBigSum(size, input_array);
30+
CHECK(result == testcase["expected"]);
31+
}
32+
}
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+
]

0 commit comments

Comments
 (0)