-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from sir-gon/feature/a_very_big_sum
[Hacker Rank]: Warmup: A Very Big Sum. Solved ✅.
- Loading branch information
Showing
5 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
7 changes: 7 additions & 0 deletions
7
src/lib/exercises/include/exercises/hackerrank/warmup/a_very_big_sum.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#include <vector> | ||
|
||
#pragma once | ||
|
||
namespace hackerrank::warmup { | ||
long aVeryBigSum(const std::vector<long>& ar); | ||
} |
17 changes: 17 additions & 0 deletions
17
src/lib/exercises/src/hackerrank/warmup/a_very_big_sum.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include <exercises/hackerrank/warmup/a_very_big_sum.hpp> | ||
|
||
/** | ||
* @link Problem definition [[docs/hackerrank/warmup/a_very_big_sum.md]] | ||
*/ | ||
|
||
#include <numeric> | ||
#include <vector> | ||
|
||
namespace hackerrank::warmup { | ||
|
||
long aVeryBigSum(const std::vector<long>& ar) { | ||
const long INIT_VALUE = 0L; | ||
return std::accumulate(ar.begin(), ar.end(), INIT_VALUE); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include <catch2/catch_test_macros.hpp> | ||
|
||
#include <exercises/hackerrank/warmup/a_very_big_sum.hpp> | ||
#include <iostream> | ||
#include <vector> | ||
|
||
#include <filesystem> | ||
#include <fstream> | ||
#include <nlohmann/json.hpp> | ||
using json = nlohmann::json; | ||
|
||
TEST_CASE("aVeryBigSum JSON Test Cases", "[warmup]") | ||
{ | ||
std::filesystem::path cwd = std::filesystem::current_path(); | ||
std::string path = cwd.string() + "/unit/lib/hackerrank/warmup/a_very_big_sum.testcases.json"; | ||
|
||
INFO("aVeryBigSum JSON test cases FILE: " << path); | ||
|
||
std::ifstream f(path); | ||
json data = json::parse(f); | ||
|
||
for (auto testcase : data) { | ||
long result = hackerrank::warmup::aVeryBigSum(testcase["input"]); | ||
CHECK(result == testcase["expected"]); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/tests/unit/lib/hackerrank/warmup/a_very_big_sum.testcases.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[ | ||
{ | ||
"input": [1000000001, 1000000002, 1000000003, 1000000004, 1000000005], | ||
"expected": 5000000015 | ||
} | ||
] |