Skip to content

Commit da8896c

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank]: Compare the Triplets solved ✓
1 parent 702cb77 commit da8896c

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

docs/hackerrank/warmup/aVeryBigSum.md

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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/warmup/aVeryBigSum.md]]
3+
*/
4+
5+
package hackerrank
6+
7+
import (
8+
utils "gon.cl/algorithm-exercises/src/utils"
9+
)
10+
11+
func AVeryBigSum(ar []int) int {
12+
var result = 0
13+
14+
for i := 0; i < len(ar); i++ {
15+
result += ar[i]
16+
}
17+
18+
utils.Info("aVeryBigSum answer => %d", result)
19+
return result
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package hackerrank
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestAVeryBigSum(t *testing.T) {
11+
12+
var input = []int{1000000001, 1000000002, 1000000003, 1000000004, 1000000005}
13+
const expectedSolution = 5000000015
14+
15+
testname := fmt.Sprintf("solveMeFirst(%d) => %d \n", input, expectedSolution)
16+
t.Run(testname, func(t *testing.T) {
17+
18+
ans := AVeryBigSum(input)
19+
assert.Equal(t, expectedSolution, ans)
20+
})
21+
}

0 commit comments

Comments
 (0)