Skip to content

Commit 886cc3a

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank]: A Very Big Sum solved ✓
1 parent d77ebd2 commit 886cc3a

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace algorithm_exercises_csharp.hackerrank;
2+
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
5+
[TestClass]
6+
public class AVeryBigSumTest
7+
{
8+
public class AVeryBigSumTestCase
9+
{
10+
public List<long> ar = [];
11+
public long expected = 0;
12+
}
13+
14+
private static readonly AVeryBigSumTestCase[] tests = [
15+
new()
16+
{
17+
ar = [1000000001, 1000000002, 1000000003, 1000000004, 1000000005],
18+
expected = 5000000015
19+
}
20+
];
21+
22+
[TestMethod]
23+
public void testSimpleArraySum()
24+
{
25+
long result;
26+
27+
foreach (AVeryBigSumTestCase test in tests)
28+
{
29+
result = AVeryBigSum.aVeryBigSum(test.ar);
30+
Assert.AreEqual(test.expected, result);
31+
}
32+
}
33+
}
34+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// @link Problem definition [[docs/hackerrank/warmup/a_very_big_sum.md]]
2+
3+
namespace algorithm_exercises_csharp.hackerrank;
4+
5+
using System.Diagnostics.CodeAnalysis;
6+
7+
public class AVeryBigSum
8+
{
9+
[ExcludeFromCodeCoverage]
10+
protected AVeryBigSum() { }
11+
12+
public static long aVeryBigSum(List<long> _ar)
13+
{
14+
var total = 0L;
15+
16+
foreach (long x in _ar)
17+
{
18+
total += x;
19+
}
20+
21+
return total;
22+
}
23+
}
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.

0 commit comments

Comments
 (0)