Skip to main content
Code Review

Return to Answer

added 499 characters in body
Source Link
Todd Lehman
  • 743
  • 4
  • 11

Your logic looks solid to me. It's subtle, though.

Here is another version using long, but with much simpler logic:

public static boolean canAdd(int... values) {
 long longSum = 0;
 int intSum = 0;
 for (final int value: values) {
 intSum += value;
 longSum += value;
 }
 return intSum == longSum;
}

That's the most straightforward way I can think to write it — and it's going to be pretty definitive.

Note that there is no "early out" in this loop, meaning it will always run to the end of the list. However, not having any conditionals, it's likely to be faster in many cases, if that matters.

(6 years later) Here is an updated version inspired by user 'cellepo' that stops as soon as it detects overflow, in order to avoid false positives (possible in the earlier version if the list of values was in the billions):

public static boolean canAdd(int... values) {
 long longSum = 0;
 int intSum = 0;
 for (final int value: values) {
 intSum += value;
 longSum += value;
 if (intSum != longSum)
 return false;
 }
 return true;
}

Your logic looks solid to me. It's subtle, though.

Here is another version using long, but with much simpler logic:

public static boolean canAdd(int... values) {
 long longSum = 0;
 int intSum = 0;
 for (final int value: values) {
 intSum += value;
 longSum += value;
 }
 return intSum == longSum;
}

That's the most straightforward way I can think to write it — and it's going to be pretty definitive.

Note that there is no "early out" in this loop, meaning it will always run to the end of the list. However, not having any conditionals, it's likely to be faster in many cases, if that matters.

Your logic looks solid to me. It's subtle, though.

Here is another version using long, but with much simpler logic:

public static boolean canAdd(int... values) {
 long longSum = 0;
 int intSum = 0;
 for (final int value: values) {
 intSum += value;
 longSum += value;
 }
 return intSum == longSum;
}

That's the most straightforward way I can think to write it. Note that there is no "early out" in this loop, meaning it will always run to the end of the list. However, not having any conditionals, it's likely to be faster in many cases, if that matters.

(6 years later) Here is an updated version inspired by user 'cellepo' that stops as soon as it detects overflow, in order to avoid false positives (possible in the earlier version if the list of values was in the billions):

public static boolean canAdd(int... values) {
 long longSum = 0;
 int intSum = 0;
 for (final int value: values) {
 intSum += value;
 longSum += value;
 if (intSum != longSum)
 return false;
 }
 return true;
}
Source Link
Todd Lehman
  • 743
  • 4
  • 11

Your logic looks solid to me. It's subtle, though.

Here is another version using long, but with much simpler logic:

public static boolean canAdd(int... values) {
 long longSum = 0;
 int intSum = 0;
 for (final int value: values) {
 intSum += value;
 longSum += value;
 }
 return intSum == longSum;
}

That's the most straightforward way I can think to write it — and it's going to be pretty definitive.

Note that there is no "early out" in this loop, meaning it will always run to the end of the list. However, not having any conditionals, it's likely to be faster in many cases, if that matters.

lang-java

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