###Overflow issues###
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.
###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.
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.
###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.