-2

Body:

I am trying to solve the “A Very Big Sum” problem on HackerRank using Java.
The task is to sum large integers (long values), but my output is not matching the expected answer.

Here is my code:

import java.io.*;
import java.util.*;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long sum = 0;

        for (int i = 0; i < n; i++) {
            long val = sc.nextLong();
            sum += val;
        }

        System.out.println(sum);
    }
}

What I expected:

The code should correctly sum all the long values and print the total.

What is happening instead:

Even though I’m using long, the output for some test cases is still incorrect (either overflow-like wrong values or mismatched totals).

What I tried:

  • Printing each value to confirm input

  • Replacing int n with long n

  • Checking loop logic

  • Running sample test cases manually

My question:

Why is my solution giving incorrect results for some test cases, even though I’m using long?
Is there something wrong with how I’m reading input or summing the values?

New contributor
Anurag Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
5
  • 4
    long is only 64bit, their tests require bigger values, use BigInteger or implement your own long math logic Commented 5 hours ago
  • You mention "overflow-like wrong values", have you debugged this? Commented 3 hours ago
  • 1
    Could you state the precise requirements? How big may the numbers be? How many of them do you need to add? A link o the HackerRank challenge may be a nice supplement, Commented 2 hours ago
  • 1
    I cannot reproduce. I ran your code, entered 10 and ten times 10000000000 (10 to the power of 10). I expected 100000000000, which was also the output I got. Commented 2 hours ago
  • For reference, the HackerRank challenge in question appears to be: hackerrank.com/challenges/a-very-big-sum/problem. It also says "long int" should work Commented 2 hours ago

1 Answer 1

-1
import java.math.BigInteger;

BigInteger sum = BigInteger.ZERO;

for (int i = 0; i < n; i++) {
    BigInteger val = sc.nextBigInteger();
    sum = sum.add(val);
}

System.out.println(sum);
New contributor
Neerav is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Sign up to request clarification or add additional context in comments.

1 Comment

This doesn’t attempt to answer their questions: “ Why is my solution giving incorrect results for some test cases, even though I’m using long? Is there something wrong with how I’m reading input or summing the values?”

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.