Write a function that takes an array as input and returns an array of 2 numbers. The returned array contains the sum of even numbers and sum of odd numbers from the input.
If any of the input is null it should be treated as an empty array
Example: Input:
[30, 18, 2, 83, 20, 71]
Output:
[70, 154]Input:
[14, 11, 10, 67, 41]
Output:
[24, 119]Input: [36, 24, -82, 29, 44, -3, -100, -5, 49] Output: [-78, 70]
The function that I have written is
public int[] getSumOfEvensAndOdds(int[] input) {
int x[] = input;
int even = 0, odd = 0;
for (int i = 0; i < x.length; i++) {
if (x[i] % 2 == 0)
even += x[i];
else
odd += x[i];
}
int[] ans={even, odd};
return ans;
}
But how should I incorporate the part of the empty array?