I've been studying python algorithm and would like to solve a problem that is:
- A positive integer array A and an integer K are given.
- Find the largest even sum of the array A with K elements.
- If not possible, return -1.
For example, if there is an array A= [1,2,3,4,4,5] and K= 3, the answer is 12 (5+4+3), which is the largest even sum with K (3) elements. However, if A= [3, 3, 3] and K= 1, the answer is -1 because it cannot make an even sum with one element.
I tried to exclude every minimum odd from the array, but it failed when K=n in the while loop. Is there any simple way to solve this problem? I would sincerely appreciate if you could give some advice.