Problem Statement
A description of the problem can be found on Hackerrank.
Solution
Read from input and sum all numbers.
Same as Simple Array Sum, but you have to use bigger integer type as 32-bit integer if language does not support automatic conversion to Big Integers as default
I created solution in 4 languages:
All solutions are also available on my GitHub.
Scala
1 2 3 4 5 |
object AVeryBigSum 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 14 15 16 17 |
import java.util.*; public class AVeryBigSum { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int numbers = Integer.parseInt(stdin.nextLine()); String[] integers = stdin.nextLine().split(" "); long sum = 0; for(int i = 0; i < numbers; i++) { sum += Long.parseLong(integers[i]); } System.out.println(sum); stdin.close(); } } |
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 |