We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Loading...
  • Practice
  • Compete
  • Jobs
  • Leaderboard
  1. Practice
  2. Algorithms
  3. Warmup
  4. A Very Big Sum
  5. Discussions

A Very Big Sum

  • Problem
  • Submissions
  • Leaderboard
  • Discussions
  • Editorial

    You are viewing a single comment's thread. Return to all comments →

  • VRatr 3 years ago+ 3 comments

    This was not the case in C++ if you're using accumulate().

    long long int total = accumulate(arr.begin(),arr.end(), 0);
    

    which appears to overflow. I just iterated through the elements in a loop and summed the total instead.

    12|
    ParentPermalink
    • gautam11681 3 years ago+ 3 comments

      Have you figured out why this happens? I even tried passing a custom function to do the addition and still it overflows!

      -4|
      ParentPermalink
      • razor123 3 years ago+ 2 comments

        The problem isn't meant to be solved using long integer. Use integer datatype to solve it. You may divide the bits and then add them seperately.

        -1|
        ParentPermalink
        • gautam11681 3 years ago+ 0 comments

          well i think you are forgetting about kiss

          6|
          ParentPermalink
        • havoklord 2 years ago+ 0 comments

          It is a problem that can be solved however you want. But if you look at the hint "When we add several integer values, the resulting sum might exceed the above range. You might need to use long long int in C/C++ or long data type in Java to store such sums. " It would contradict what you said.

          0|
          ParentPermalink
      • alextaylor 3 years ago+ 4 comments

        Because the type of the accumulator that will be used to determine the sum is based on the type of the third input argument. The literal 0 is not being interpreted as long long int. If you cast the initial value to accumulate appropriately, you get the right answer.

        accumulate(arr.begin(),arr.end(),static_cast<long long int>(0))
        
        13|
        ParentPermalink
        • alextaylor 3 years ago+ 1 comment

          From http://stackoverflow.com/questions/8108642/type-of-integer-literals-not-int-by-default

          "The type of an integer literal is the first of the corresponding list in Table 6 in which its value can be represented."

          And Table 6, for literals without suffixes and decimal constants, gives:

          int
          long int
          long long int
          

          So, without the cast, the literal 0 can be represented as int, so accumulate uses int as the type for accumulation.

          1|
          ParentPermalink
          • geodoom 3 years ago+ 1 comment

            I think there is another way , to avoid the static_cast(0) , and do it by simply accumulate(....,0L); If I remember correct (I am too lazy to search in books or google now to verify!) the L forces the number to be interpreted as long long.

            0|
            ParentPermalink
            • Gamenot 3 years ago+ 0 comments

              L is the suffix for long just like LL is the suffix for long long. Weird but not confusing. accumulate(....,0LL) avoids the static cast.

              3|
              ParentPermalink
        • C_jain 2 years ago+ 0 comments

          or we can do it more simply like this..

          accumulate(arr.begin, arr.end(), (long long int)0);

          0|
          ParentPermalink
        • trungskigoldberg 2 years ago+ 1 comment

          Hi, I understand the 3rd argument of the function accumulate(), which indicates the type of integer literal in this case. But why we need to cast statically as

          .....static_cast<long long int>(0))
          

          for the long long integer?

          0|
          ParentPermalink
          • C_jain 2 years ago+ 0 comments

            i also don't know that why i cast it explicitly

            0|
            ParentPermalink
        • dinaelhanan 5 months ago+ 0 comments

          I am having trouble grasping a clear definition of initial value. What would that mean?

          0|
          ParentPermalink
      • Kanahaiya 12 months ago+ 1 comment

        you can check out the below link for video explanation :)

        https://github.com/Java-aid/Hackerrank-Solutions

        -1|
        ParentPermalink
        • dinaelhanan 4 months ago+ 2 comments

          thanks!

          0|
          ParentPermalink
          • getarqamnow 2 weeks ago+ 1 comment

            Thanks

            0|
            ParentPermalink
            • Kanahaiya 2 weeks ago+ 0 comments

              most welcome..:)

              0|
              ParentPermalink
          • Kanahaiya 2 weeks ago+ 0 comments

            most welcome..:)

            0|
            ParentPermalink
    • Vidya35 1 year ago+ 0 comments

      long int result=0; cout<

      This works for me in C++. No overflow.

      -1|
      ParentPermalink
    • msaad1200 6 months ago+ 1 comment
      return accumulate(ar.begin(), ar.end(), 0ll);
      

      notice the ll after 0

      0|
      ParentPermalink
      • dinaelhanan 5 months ago+ 0 comments

        just wondering, why is the initial value 0ll? What does that mean? I have done a search on it and all I am understanding is that the double L means long long. Also, I am having trouble understading a clear definition of initial value. From the tutorials I see, it looks like the value before the first value in the array or vector. Is that wrong?

        0|
        ParentPermalink
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature