Intermediatecoding Verified
Given an array of integers and K, find a subsequence of length K with the maximum possible even sum, or return -1 if no even sum exists.
ReportedSource: GeeksforGeeks interview experience: Snowflake USA internship 2021
Quick answerFree
Take the K largest; if the sum is odd, try swapping the smallest chosen odd with the largest unchosen even, or the smallest chosen even with the largest unchosen odd; best valid result or -1. O(n log n).
Detailed answer· ~130 words
Interview PrepUnlock the full answer with Interview Prep
Get a ~130-word model answer and a runnable reference solution, plus 28 more Algorithms questions.
Also in Pro (₹499 / 90 days).
Reference solution· runs in the browser sandbox
Interview PrepUnlock with Interview Prep
+ 28 more Algorithms questions in Interview Prep, each with the full answer.