Sum of Largest Odd and Even Numbers
Given an array of integers, the task is to find the sum of the largest odd number and the largest even number from the array. If no odd or even numbers are present, treat the missing value as 0 in the sum.
[ 3, 5, 2, 8, 1 ]
Explanation. The largest even number in the array is 8 and the largest odd number is 5, thus the sum is 13.
[ 7, -1, 3, -5 ]
Explanation. The largest even number is not present. The largest odd number is 7, thus the sum is 0 (even) + 7 (odd) = 7.
[ -2, -4, -6, 0 ]
Explanation. The largest odd number is not present. The largest two even numbers are 0 and -2, and the larger is 0, thus the sum is 0 + -2 = -2.
[ 9 ]
Explanation. Only one odd number 9 is present and no even numbers, thus the sum is 0 (even) + 9 (odd) = 9.
[ 10, 2, -20 ]
Explanation. Only even numbers are present. The largest even number is 10, thus the sum is 10 (even) + 0 (odd) = 10.
Follow-up: Consider how you would handle arrays with very large numbers or very large arrays, optimizing performance.
The array will have at least one integer, and integer values may range from negative to positive.
- Views
- 4