Skip to content

Commit e1be732

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank]: Simple Array Sum solved ✓
1 parent b06a77e commit e1be732

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package ae.hackerrank;
2+
3+
import java.util.List;
4+
5+
/**
6+
* Simple Array Sum.
7+
*
8+
* @link Problem definition [[docs/hackerrank/warmup/simple_array_sum.md]]
9+
*/
10+
public class SimpleArraySum {
11+
12+
private SimpleArraySum() {}
13+
14+
static java.util.logging.Logger logger = util.CustomLogger.getLogger();
15+
16+
/**
17+
* simpleArraySum.
18+
*/
19+
public static int simpleArraySum(List<Integer> ar) {
20+
int total = 0;
21+
22+
for (Integer name : ar) {
23+
total += name;
24+
}
25+
26+
String log = String.format("simpleArraySum solved: %d", total);
27+
logger.info(log);
28+
29+
return total;
30+
}
31+
}
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 SimpleArraySumTest {
11+
12+
@Test void testSimpleArraySum() {
13+
14+
Integer answer = 5;
15+
List<Integer> ar = Arrays.asList(2, 3);
16+
17+
Integer solutionFound = SimpleArraySum.simpleArraySum(ar);
18+
19+
assertEquals(answer, solutionFound,
20+
String.format("Problem 0000 answer must be: %d", answer)
21+
);
22+
}
23+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# [Simple Array Sum](https://www.hackerrank.com/challenges/simple-array-sum/problem)
2+
3+
Difficulty: #easy
4+
Category: #warmup
5+
6+
Given an array of integers, find the sum of its elements.
7+
For example, if the array $ ar = [1, 2, 3], 1 + 2 + 3 = 6 $, so return $ 6 $.
8+
9+
## Function Description
10+
11+
Complete the simpleArraySum function in the editor below. It must return the sum
12+
of the array elements as an integer.
13+
simpleArraySum has the following parameter(s):
14+
15+
- ar: an array of integers
16+
17+
## Input Format
18+
19+
The first line contains an integer, , denoting the size of the array.
20+
The second line contains space-separated integers representing the array's elements.
21+
22+
## Constraints
23+
24+
$ 0 < n, ar[i] \leq 1000 $
25+
26+
## Output Format
27+
28+
Print the sum of the array's elements as a single integer.
29+
30+
## Sample Input
31+
32+
```text
33+
6
34+
1 2 3 4 10 11
35+
```
36+
37+
## Sample Output
38+
39+
```text
40+
31
41+
```
42+
43+
## Explanation
44+
45+
We print the sum of the array's elements: $ 1 + 2 + 3 + 4 + 10 + 11 = 31 $.

0 commit comments

Comments
 (0)