Skip to main content
Code Review

Return to Revisions

2 of 2
Commonmark migration

Overflow issues

If the largest two integers added together exceed the maximum integer value, you will not come up with the correct answer. For example, 2000000000 + 2000000000 becomes some negative value and you would miss that as the answer. To do this correctly you should do the addition using long and store the max value in a long as well:

 long currentMax = (long) array[i] + (long) array[j];

Even if you used the better algorithm of finding the two largest values in the array, you would still need to return a long in order to return the correct sum.

JS1
  • 28.9k
  • 3
  • 41
  • 83
default

AltStyle によって変換されたページ (->オリジナル) /