Skip to content

A Very Big Sum

Posted in Education, and WhoCodeFirst

Calculate and print the sum of the elements in an array, keeping in mind that some of those integers may be quite large.

Function Description:

Complete the aVeryBigSum function in the editor below. It must return the sum of all array elements.

aVeryBigSum has the following parameter(s):

ar: an array of integers.

Input Format:

The first line of the input consists of an integer.
The next line contains space-separated integers contained in the array.

Output Format:

Print the integer sum of the elements in the array.

My Solution:

‘use strict’;

const fs = require(‘fs’);

process.stdin.resume();
process.stdin.setEncoding(‘utf-8’);

let inputString = ”;
let currentLine = 0;

process.stdin.on(‘data’, inputStdin => {
inputString += inputStdin;
});

process.stdin.on(‘end’, _ => {
inputString = inputString.replace(/\s*$/, ”)
.split(‘\n’)
.map(str => str.replace(/\s*$/, ”));

main();
});

function readLine() {
return inputString[currentLine++];
}

function aVeryBigSum(ar) {
var sum=0;
for(let i=0;i<ar.length;i++){
sum = sum + ar[i];
}
return sum;
}

function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

const arCount = parseInt(readLine(), 10);

const ar = readLine().split(‘ ‘).map(arTemp => parseInt(arTemp, 10));

let result = aVeryBigSum(ar);

ws.write(result + “\n”);

ws.end(); }

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *