HackerRank A Very Big Sum Problem Solution
In this post, We are going to solve HackerRank A Very Big Sum Problem. 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.
Constraints
1<n<10
0<ar[I]<10 Power of 10
A Very Big Sum C Solution
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int t;
long int res;
scanf("%d",&t);
int t1;
for(int i = 0;i<t;i++){
scanf("%d",&t1);
res = res + t1;
printf("");
}
printf("%ld",res);
return 0;
}
A Very Big Sum C++ Solution
#include <iostream>
using namespace std;
int main()
{
unsigned long long int n,sum=0;
cin >> n;
unsigned long long int *a = new unsigned long long int[n];
for (int i=0; i<n;i++){
cin >> *(a+i);
sum+=*(a+i);
}
cout << sum;
}
A Very Big Sum C Sharp Solution
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
static void Main(String[] args) {
int t = Convert.ToInt32(Console.ReadLine());
string elements = Console.ReadLine();
List<int> numbers = elements.Split(' ').Select(Int32.Parse).ToList();
long total= 0;
foreach(int n in numbers )
total+=n;
Console.WriteLine(total);
}
}
A Very Big Sum Java Solution
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'aVeryBigSum' function below.
*
* The function is expected to return a LONG_INTEGER.
* The function accepts LONG_INTEGER_ARRAY ar as parameter.
*/
public static long aVeryBigSum(List<Long> ar) {
Long sum = 0L;
for (Long a : ar) {
sum += a;
}
return sum;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int arCount = Integer.parseInt(bufferedReader.readLine().trim());
List<Long> ar = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Long::parseLong)
.collect(toList());
long result = Result.aVeryBigSum(ar);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}
A Very Big Sum JavaScript Solution
function processData(input) {
var split = input.split('\n');
var count = split[0];
var numbers = split[1].split(' ');
var sum = 0;
for(var i = 0; i < count; i++){
sum += parseInt(numbers[i], 10);
}
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);
});
A Very Big Sum Python Solution
import sys
def read_ints(n):
#print("n:", n)
result = 0
idx = 0
while idx <= n:
c = sys.stdin.read(1)
if not c: break
if c == chr(10): break
if c == chr(32):
#print("idx:", idx)
#print('result:', result)
yield result
idx += 1
result = 0
continue
result = 10 * result + (ord(c) - ord('0'))
if result:
#print('result:', result)
yield result
n = next(read_ints(1))
sum = 0
for i in read_ints(n):
sum += i
print(sum)
Other Solutions