Skip to content

Commit 42fee11

Browse files
authored
Merge pull request #161 from sir-gon/feature/a_very_big_sum
Feature/a very big sum
2 parents 0681ad3 + 87b1d00 commit 42fee11

File tree

4 files changed

+108
-1
lines changed

4 files changed

+108
-1
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package ae.hackerrank;
2+
3+
import java.util.List;
4+
5+
/**
6+
* VeryBigSum.
7+
*
8+
* @link Problem definition [[docs/hackerrank/warmup/a_very_big_sum.md]]
9+
*/
10+
public class VeryBigSum {
11+
12+
private VeryBigSum() {
13+
}
14+
15+
static java.util.logging.Logger logger = util.CustomLogger.getLogger();
16+
17+
/**
18+
* aVeryBigSum.
19+
*/
20+
public static long aVeryBigSum(List<Long> ar) {
21+
long total = 0L;
22+
23+
for (long x : ar) {
24+
total += x;
25+
}
26+
27+
return total;
28+
}
29+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package ae.hackerrank;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import org.junit.jupiter.api.Test;
8+
9+
10+
class VeryBigSumTest {
11+
12+
@Test void test_aVeryBigSum() {
13+
14+
Long answer = 5000000015L;
15+
List<Long> arr = Arrays.asList(1000000001L, 1000000002L, 1000000003L, 1000000004L, 1000000005L);
16+
17+
Long solutionFound = VeryBigSum.aVeryBigSum(arr);
18+
19+
assertEquals(answer, solutionFound,
20+
String.format("Problem 0000 answer must be: %d", answer)
21+
);
22+
}
23+
}

config/checkstyle/checkstyle.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@
348348
<property name="excludeScope" value="nothing"/>
349349
</module>
350350
<module name="MethodName">
351-
<property name="format" value="^[a-z][a-z0-9]\w*$"/>
351+
<property name="format" value="(^[a-z]|[A-Z0-9])[a-z]*$"/>
352352
<message key="name.invalidPattern"
353353
value="Method name ''{0}'' must match pattern ''{1}''."/>
354354
</module>
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)