Reputation: 2311
Given an array of integers I have to find the subarray with maximum sum such that the sum is odd.
For instance in the array "2,5,7" the answer is 7,2. Till now I have come across the Kadane's algorithm And its implementation here http://pastebin.com/qwWzbxKw
But how do I extend it such that the sum is odd.
EDIT
All elements of the array are Integers and positive
Upvotes: 1
Views: 1040
Reputation: 178471
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:
EDIT:
For all positive number it's even easier - if the sum is not odd - just kick the smallest odd number out.
Upvotes: 1