HackerRank.com Challenge – A Very Big Sum

In this lesson on HackerRank, I was challenged to demonstrate my knowledge of the long data type by needing to add multiple large numbers that would have caused an integer to overflow.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    static long aVeryBigSum(int n, long[] ar) {
        //Setup space on the heap to store our running total
        long total = 0;
        //Iterate over each element in array ar adding the value to the running total in total
        for(long number : ar)
            total = total + number;
        //return the sum contained in total
        return total;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        long[] ar = new long[n];
        for(int ar_i = 0; ar_i < n; ar_i++){
            ar[ar_i] = in.nextLong();
        long result = aVeryBigSum(n, ar);

Comments are closed.