Write a method haveEqualSums(int[] input1, int[] input2)
时间: 2024-01-13 09:02:44 浏览: 66
As an AI language model, I cannot write code in any specific language. However, I can provide you with the logic that can be used to implement a method that checks whether two arrays have equal sums or not. Here is the logic:
1. Check if the length of both arrays is equal. If not, return false as the arrays cannot have equal sums.
2. Initialize two variables, sum1 and sum2, to 0.
3. Traverse both arrays simultaneously and add the elements at the same index to the respective sums.
4. After traversing both arrays, compare the values of sum1 and sum2. If they are equal, return true. Otherwise, return false.
Here is an example implementation of this method in Java:
```
public boolean haveEqualSums(int[] input1, int[] input2) {
if (input1.length != input2.length) {
return false;
}
int sum1 = 0, sum2 = 0;
for (int i = 0; i < input1.length; i++) {
sum1 += input1[i];
sum2 += input2[i];
}
return sum1 == sum2;
}
```
阅读全文