A Very Big Sum HackerRank Solution in C, C++, Java, Python

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 .

The next line contains  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:

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.

A Very Big Sum HackerRank 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 HackerRank Solution in C++

#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 HackerRank 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 HackerRank 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 HackerRank Solution in C#

using System;
using System.Collections.Generic;
using System.IO;
class Solution {
    static void Main(String[] args) {
        int N = Convert.ToInt32(Console.ReadLine());
        string[] array = Console.ReadLine().Split(' ');
        double sum = 0;
        
        for (int i = 0; i < N; i++)
            sum += Convert.ToInt32(array[i]);
        
        Console.WriteLine(sum);
    }
}

 

Attempt – A Very Big Sum HackerRank Challenge

Link: https://www.hackerrank.com/challenges/a-very-big-sum

Next Challenge – Diagonal Difference HackerRank Solution

Link: https://exploringbits.com/diagonal-difference-hackerrank-solution/

Leave a Comment