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.
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.
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))
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.
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
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?
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".
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 :
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.
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.
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:
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??
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).
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.
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 ............
A Very Big Sum
You are viewing a single comment's thread. Return to all comments →
I completely agree with you. I am using Python, and all I had to do was copy and paste.
This was not the case in C++ if you're using accumulate().
which appears to overflow. I just iterated through the elements in a loop and summed the total instead.
Have you figured out why this happens? I even tried passing a custom function to do the addition and still it overflows!
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.
well i think you are forgetting about kiss
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.
Thank you so much for giving the correct info, as was searching long and nobody was telling about c++/c solution.
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.
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:
So, without the cast, the literal 0 can be represented as int, so accumulate uses int as the type for accumulation.
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.
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.or we can do it more simply like this..
accumulate(arr.begin, arr.end(), (long long int)0);
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
for the long long integer?
i also don't know that why i cast it explicitly
I am having trouble grasping a clear definition of initial value. What would that mean?
Thankyou @alextaylor , it works now.
you can check out the below link for video explanation :)
https://github.com/Java-aid/Hackerrank-Solutions
thanks!
Thanks
most welcome..:)
most welcome..:)
long int result=0; cout<
This works for me in C++. No overflow.
notice the ll after 0
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?
Aha, so this was how you did it with accumulate. Thank you!
What role does the accumulate function exactly play here?? and also what is the meaning of this "ll" at the end?
@gurleenkhalsa03 Read the C++ documentation to
std::accumulate
in the provided list. Accumulate is used to sum vectors.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".
what is 0 or 0ll here
work!
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 :
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.
Your absolutely true@VRatr , accumulate does not work :( This is the most effecient way though :(
Even that doesnt work for me
Had you used plain old loop to add, there would have been no error. accumulate may not work here as array has int members.
Also possible with single
l
:From https://en.cppreference.com/w/cpp/algorithm/accumulate:
Same here with Ruby. I had to re-read the problem just to make sure that I wasn't reading the same thing.
don't use accumulate, just loop through array.
here is problem solution in java python c++ c and javascript programming.
HackerRank A Very Big Sum solution
Here is my c++ solution, explanatino here : https://youtu.be/k6H_RljnY8g
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; }
wow!
cool!! was missing 'li' and 'ld' in the scanf statements...'l' for long..!! thank you.
Thanks!
not working
Here's my code in C
*
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:
see full and easy to understand code on:
https://github.com/akkshaychauhan/hacckerank-code/blob/master/Algorithms:A%20Very%20Big%20Sum
Not working ...is there any place where I can see answers ?
https://github.com/akkshaychauhan/hacckerank-code
Try long long int in place of int if you are using c
take value as value[] it will surly work.
you can check out the below link for video explanation :)
https://github.com/Java-aid/Hackerrank-Solutions
thanks buddy , i learned something from your program :-)
I am glad to know that my work helped you in your learning.
thank you it worked
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??
I want to use functions in it . Can you give me the idea of it.
you can check out the below link for video explanation :)
https://github.com/Java-aid/Hackerrank-Solutions
It will only run for numbers less than 10 digit not more than that.
"a world of pain" - completely agree with you.
Does this work if you put the sum part under that function they have provided
THanks dude
in C my code gave a timeout issue :(
what is arr_i ?
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).
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.
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:
please !!! can you explain the code. which you provided here.
super
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 ............
here is solution of a very big sum
github: https://github.com/nick-mehta/hackerrank
return sum(ar)
and it's done in Python
bro would u explain how to solve or would you send me the code please.