Problem Statement
A description of the problem can be found on Hackerrank.
Solution
As this challenge is in Warmup subdomain, solution is pretty easy. The only thing to do is to be able to read from input given n
times and make sum of all numbers.
I created solution in 4 languages:
All solutions are also available on my GitHub.
Scala
1 2 3 4 5 |
object SimpleArraySum extends App { val console = io.Source.stdin.bufferedReader() val n = console.readLine().toInt; println(console.readLine().split(" ").map(_.toInt).sum) } |
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.Scanner; public class SimpleArraySum { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); final int n = stdin.nextInt(); int sum = 0; for(int i = 0; i < n; i++) { sum += stdin.nextInt(); } System.out.println(sum); } } |
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
var sum = 0; function processData(input) { var lines = input.split("\n"); var n = parseInt(lines[0]); var arr = lines[1].split(" "); for (var i = 0; i < n; i++) { sum += parseInt(arr[i]); } console.log(sum); } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); }); |
Ruby
1 2 3 4 5 6 7 |
n = gets.to_i arr = gets.split(" ").map { |c| c.to_i } sum = 0 for i in arr do sum += i end puts sum |