Sum of Largest Odd and Even Numbers
Given an array of integers, your task is to find the largest odd number and the largest even number in the array. Once identified, return the sum of these two numbers.
[ 1, 2, 3, 4, 5 ]
Explanation. The largest odd number is 5, and the largest even number is 4. Their sum is 9.
[ 10, 21, 4, 7 ]
Explanation. The largest odd number is 21, and the largest even number is 10. Their sum is 31.
[ -1, -2, -3, -4 ]
Explanation. The largest odd number is -1, and the largest even number is -2. Their sum is -3.
[ 8 ]
Explanation. There's only one number in the array which is even, so it's also the largest even number. No odd number exists, hence the sum is 8 (even number itself).
Follow-up: Think about how you can solve the problem if the input rule is changed to include numbers of any kind, not just integers. What checks and changes would be necessary?
The input array will contain at least one integer. Each integer is in the range of standard 32-bit signed integers.
- Views
- 3