Sum of Largest Odd and Smallest Even
Given an array of integers, your task is to find the sum of the largest odd number and the smallest even number in the array. If there are no odd numbers or no even numbers in the array, return -1.
[ 1, 2, 3, 4, 5, 6 ]
Explanation. The largest odd number in the array is 5 and the smallest even number is 2. Their sum is 7.
[ 11, 21, 45, 78, 88 ]
Explanation. The largest odd number is 45 and the smallest even number is 78. Their sum is 113.
[ 2, 4, 6, 8 ]
Explanation. There are no odd numbers in the array.
[ 1, 3, 5, 7 ]
Explanation. There are no even numbers in the array.
[ 33, 18, 77, 10, 63 ]
Explanation. The largest odd number is 77 and the smallest even number is 10. Their sum is 83.
Follow-up: How would you optimize your solution if the size of the array is very large?
The input array will contain at least one element. All elements will be integers.
- Views
- 4