A Very Big Sum

  • + 9 comments

    I completely agree with you. I am using Python, and all I had to do was copy and paste.

    • + 9 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.

      • + 3 comments

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

        • + 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.

          • + 0 comments

            well i think you are forgetting about kiss

          • + 1 comment

            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 comments

              Thank you so much for giving the correct info, as was searching long and nobody was telling about c++/c solution.

        • + 5 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))
          
          • + 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 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 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.

          • + 0 comments

            or we can do it more simply like this..

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

          • + 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 comments

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

          • + 0 comments

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

          • + 0 comments

            Thankyou @alextaylor , it works now.

        • + 1 comment

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

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

          • + 2 comments

            thanks!

            • + 1 comment

              Thanks

              • + 0 comments

                most welcome..:)

            • + 0 comments

              most welcome..:)

      • + 0 comments

        long int result=0; cout<

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

      • + 6 comments
        return accumulate(ar.begin(), ar.end(), 0ll);
        

        notice the ll after 0

        • + 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 comments

          Aha, so this was how you did it with accumulate. Thank you!

        • + 2 comments

          What role does the accumulate function exactly play here?? and also what is the meaning of this "ll" at the end?

          • + 0 comments

            @gurleenkhalsa03 Read the C++ documentation to std::accumulate in the provided list. Accumulate is used to sum vectors.

          • + 0 comments

            Here is a nice version of comments, accumulate() takes a initial sum value for its third parameter as if you would start a function with "long sum = 0". LL at the end of 0 is a suffix standing for "long long".

        • + 0 comments

          what is 0 or 0ll here

        • + 0 comments

          work!

      • + 0 comments

        You can solve this issue with ease. The pitfall with accumulate is that it assumes the return type to be the type of the initial value of the sum ( the third argument passed to accumulate ) so , this would work :

        long long int total = accumulate(arr.begin(),arr.end(), long long 0);
        or
        long long int total = accumulate(arr.begin(),arr.end(), 0LL );
        
      • + 0 comments

        You need to do the above like return std::accumulate(ar.begin(), ar.end(), 0.0l); The last argument is the init value and its data type matters so need to send 0.0l.

      • + 0 comments

        Your absolutely true@VRatr , accumulate does not work :( This is the most effecient way though :(

      • + 0 comments

        Even that doesnt work for me

      • + 0 comments

        Had you used plain old loop to add, there would have been no error. accumulate may not work here as array has int members.

      • + 0 comments

        Also possible with single l:

        return accumulate(ar.begin(), ar.end(), 0L);
        

        From https://en.cppreference.com/w/cpp/algorithm/accumulate:

        Common mistakes If left to type inference, op operates on values of the same type as init which can result in unwanted casting of the iterator elements. For example, std::accumulate(v.begin(), v.end(), 0) likely does not give the result one wishes for when v is std::vector.

    • + 3 comments

      Same here with Ruby. I had to re-read the problem just to make sure that I wasn't reading the same thing.

      • + 0 comments

        don't use accumulate, just loop through array.

      • + 0 comments

        here is problem solution in java python c++ c and javascript programming.

        HackerRank A Very Big Sum solution

      • + 0 comments

        Here is my c++ solution, explanatino here : https://youtu.be/k6H_RljnY8g

        long aVeryBigSum(vector<long> ar) {
            long result = 0;
            for(int i = 0; i < ar.size(); i++) result += ar[i];
            return result;
        }
        
    • + 10 comments

      I tried it in pure C ...a world of pain! But here's the answer

      int main(){ int n; long bign = 0; long bignn; scanf("%d",&n); long arr[n]; for(int arr_i = 0; arr_i < n; arr_i++){ scanf("%li",&arr[arr_i]); } for(int arr_i = 0; arr_i < n; arr_i++){ bign = bign + (arr[arr_i]) ; } printf("%ld\n", bign); return 0; }

      • + 0 comments

        wow!

      • + 0 comments

        cool!! was missing 'li' and 'ld' in the scanf statements...'l' for long..!! thank you.

      • + 0 comments

        Thanks!

      • + 3 comments

        not working

        • + 1 comment
          [deleted]
        • + 0 comments

          Here's my code in C

          #include<stdio.h>
          long aVeryBigSum(long long int ,int);
          int main()
          {
              int n,i;
              long long int p;
              scanf("%d",&n);
              long long int arr[n];
              for(i=0;i<n;i++)
              {
                  scanf("%lld",&arr[i]);
              }
              p=aVeryBigSum(arr,n);
              printf("%lld",p);
          }
          long aVeryBigSum(long long int arr,int n)
          {
              long long int sum=0,i;
              for(i=0;i<n;i++)
              {
                  sum+=arr[i];
              }
              return sum;
          }
              
          

          *

      • + 7 comments

        What's the reason for using two loops and an array? I feel like that just takes too much time and too much space. Here's my take in C if anyone's interested:

        #include <stdio.h>
        
        int main(void){
            int n;
            long sum=0;
            scanf("%d", &n);
            long value;
            //long arr[n];
            for (int i=0; i < n; i++) {
                scanf("%li", &value);
                sum += value;
            }
            printf("%li", sum);
            return 0;
        }
        
        • + 1 comment

          see full and easy to understand code on:

          https://github.com/akkshaychauhan/hacckerank-code/blob/master/Algorithms:A%20Very%20Big%20Sum

          • + 3 comments

            Not working ...is there any place where I can see answers ?

            • + 0 comments

              https://github.com/akkshaychauhan/hacckerank-code

            • + 0 comments

              Try long long int in place of int if you are using c

            • + 0 comments

              take value as value[] it will surly work.

        • + 0 comments

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

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

        • + 1 comment

          thanks buddy , i learned something from your program :-)

          • + 0 comments

            I am glad to know that my work helped you in your learning.

        • + 0 comments

          thank you it worked

        • + 0 comments

          but if we intializing array in long type after then we are inputing it and summing it then it is showing runtime error.. can you tell me why this happening or i am wrong??

        • + 1 comment

          I want to use functions in it . Can you give me the idea of it.

          • + 0 comments

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

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

        • + 0 comments

          It will only run for numbers less than 10 digit not more than that.

      • + 0 comments

        "a world of pain" - completely agree with you.

      • + 0 comments

        Does this work if you put the sum part under that function they have provided

      • + 0 comments

        THanks dude

      • + 0 comments

        in C my code gave a timeout issue :(

      • + 0 comments

        what is arr_i ?

    • + 0 comments

      I'm sure there's some fancy "stuff" you could do in a strongly typed language to determine the type of the result; however, I wonder what the performance implications of that would be versus just starting with type long. Don't know why you got downvoted (I don't write in Python).

    • + 0 comments

      Yeah, it seems like the utility of the problem is language dependent. Python 3 doesn't differentiate between int and long types; they're handled exactly the same way from the users perspective.

    • + 1 comment

      Yeah but your missing the point. Firstly a number of people are coming here with little to no experience and this is great practice for them.

      The problem is clearly labeled as being easy, however maybe that didn't exist 2 years ago when this post was made.

      And for people with more experience, it is an opportunity to play around.

      Case in point:

      def aVeryBigSum(n, ar):
          return reduce(lambda x, y: x + y, ar, 0)
      
      def reduce(func, arr, s):
          for v in arr:
              s = func(s, v)
          return s
      
      • + 0 comments

        please !!! can you explain the code. which you provided here.

    • + 0 comments

      super

    • + 1 comment

      sir can you plz help me in this my code is not running it is showing indentation error at the if--main--- line but i think the indentation are correct ............

      • + 0 comments

        here is solution of a very big sum

        github: https://github.com/nick-mehta/hackerrank

    • + 1 comment

      return sum(ar)

      and it's done in Python

      • + 0 comments

        bro would u explain how to solve or would you send me the code please.