CodingSpace

[HackerRank/Algorithms] Warmup - A Very Big Sum 본문

HackerRank/Algorithm

[HackerRank/Algorithms] Warmup - A Very Big Sum

개발자_조이킴 2022. 8. 19. 09:05

Problem. Warmup - A Very Big Sum


Link.

https://www.hackerrank.com/challenges/a-very-big-sum/problem?isFullScreen=true

 

Solve Me First | HackerRank

This is an easy challenge to help you start coding in your favorite languages!

www.hackerrank.com


Description.

In this challenge, you are required to calculate and print the sum of the elements in an array,

keeping in mind that some of those integers may be quite large.

 


Key Point. 


My Answer. 

'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', function(inputStdin) {
    inputString += inputStdin;
});

process.stdin.on('end', function() {
    inputString = inputString.split('\n');

    main();
});

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

/*
 * Complete the 'aVeryBigSum' function below.
 *
 * The function is expected to return a LONG_INTEGER.
 * The function accepts LONG_INTEGER_ARRAY ar as parameter.
 */

function aVeryBigSum(ar) {
    // Write your code here
    let 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().trim(), 10);

    const ar = readLine().replace(/\s+$/g, '').split(' ').map(arTemp => parseInt(arTemp, 10));

    const result = aVeryBigSum(ar);

    ws.write(result + '\n');

    ws.end();
}

References. 

 

Comments