It appears you are not requiring it to be contiguous (from your example). This is much simpler, just take all positive elements, and if sum is odd - you are done. If it is even - remove the lowest positive odd element, or add the highest odd negative element (do which is better, it depends only on abs(highest_negative_odd)
and lowest_positive_odd
).
Pseudo code:
- sum <- sum of all positive elements
- if sum is odd - done, return the relevant subarray
- x <- highest negative odd element
- y <- lowest positive odd element
- if abs(x) < y
- sum <- sum + x //add x to the subarray
- else:
- sum <- sum - y //remove y from the subarray
- return relevant subarray
EDIT:
For all positive number it's even easier - if the sum is not odd - just kick the smallest odd number out.