Header Ad

HackerRank A Very Big Sum problem solution

In this HackerRank A Very Big Sum problem solution 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.

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):

  • int ar[n]: an array of integers.

Return

  • long: the sum of all array elements

Input Format

The first line of the input consists of an integer n.

The next line contains n space-separated integers contained in the array.

Output Format

Return the integer sum of the elements in the array.


HackerRank A Very Big Sum problem solution


Problem solution in Python programming.

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the aVeryBigSum function below.
def aVeryBigSum(ar):
    x=0
    for i in range(0,len(ar)):
        x= x + ar[i]
    return x


if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    ar_count = int(input())

    ar = list(map(int, input().rstrip().split()))

    result = aVeryBigSum(ar)

    fptr.write(str(result) + '\n')

    fptr.close()




Problem solution in Java Programming.

import java.util.Scanner;

public class BigSum {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        long sum = 0;
        while (N-- > 0 ) {
            sum += scanner.nextInt();
        }
        System.out.println(sum);
    }
}



Problem solution in C++ programming.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;


int main() {
    int t;
    double res=0;
    cin>>t;
    int table[5];
    for(int i=0;i<t;i++){
        cin>>table[i];
        res+=table[i];
    }
    cout<<fixed<<std::setprecision(0)<<res;
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    return 0;
}



Problem solution in C programming.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

   long long int arr[10];
    int n,i;
    scanf("%d",&n);
        long long int sum;
    sum=0;
    for(i=0;i<n;i++)
        scanf("%lli",&arr[i]);
     for(i=0;i<n;i++)
         {
         sum=sum+arr[i];
     }
    printf("%lli",sum);
    return 0;
}



Problem solution in JavaScript programming.

process.stdin.resume();
process.stdin.setEncoding('ascii');

var bob = "";
var joe = "";
var tom = 0;

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

process.stdin.on('end', function () {
    joe = bob.split("\n");
    var res=0;
    var n = parseInt(joe[tom].trim(), 10);
    tom += 1;
    var _line = joe[tom].trim();
    var line = _line.split(" ");
    for (var i = 0; i<n;i++) {
        res+=parseInt(line[i],10);
    }
    process.stdout.write(res);
});


Post a Comment

0 Comments