Hello Programmers, In this post, you will know how to solve the HackerRank A Very Big Sum solution. This problem is a part of the HackerRank Algorithms Series.
One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.
HackerRank A Very Big Sum Solution
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] <= 1010
Sample Input
5
1000000001 1000000002 1000000003 1000000004 1000000005
Output
5000000015
Note:
The range of the 32-bit integer is
The range of the 32-bit integer is (–231) to (231 – 1) or [-2147483648, 2147483647].
When we add several integer values, the resulting sum might exceed the above range. You might need to use long int C/C++/Java to store such sums.
HackerRank A Very Big Sum solution
A Very Big Sum solution in C
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { int t,i; scanf("%d",&t); long int arr[1000]; long int s=0; for(i=0;i<t;i++) { scanf("%ld",&arr[i]); } for(i=0;i<t;i++) { s=s+arr[i]; } printf("%ld",s); /* Enter your code here. Read input from STDIN. Print output to STDOUT */ return 0; }
A Very Big Sum solution in Cpp
#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { int n; cin>>n; unsigned long long int sum=0,in; for(int i=0;i<n;i++) {cin>>in; sum+=in;} cout<<sum; return 0; }
A Very Big Sum solution in Java
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); long sum = 0; while(t-- > 0){ sum += s.nextInt(); } System.out.println(sum); } }
A Very Big Sum solution in Python
import math import sys ans = 0 for linenum, testnum in enumerate(sys.stdin): if linenum == 1: for num in testnum.split(): ans += int(num) print ans
A Very Big Sum solution using JavaScript
function processData(input) { var length = input[0]; var sum = 0; var fullstring = ""; for(var i = 2; i < input.length; i++){ fullstring += input[i]; } var splitstring = fullstring.split(" "); for(var j = 0; j < splitstring.length; j++){ sum += parseInt(splitstring[j]); } console.log(sum); return; } 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 solution in Scala
object Solution extends App{ val n = readInt val sum = readLine.split(" ").take(n).map(_.toLong).sum println(sum) }
A Very Big Sum solution in Pascal
Program Third; Var i, n: Integer; a: LongInt; sum: Int64; Begin Readln(n); sum:= 0; For i:= 1 To n Do Begin Read(a); sum:= sum + a; End; Writeln(sum); End.
Disclaimer: This problem (A Very Big Sum) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.