Spent way too long second-guessing what 'median' means for even-length subsequences before just going with lower-middle index.
First, clarify that a subsequence preserves the original order, but since we only care about the multiset of chosen elements, we can sort the array and consider any k elements. Then, the median of a sorted subsequence of length k is the element at index floor((k-1)/2) (0-indexed) in that subsequence. To maximize the median, choose the largest possible element that can serve as the median, which is the element at index n - ceil(k/2) in the sorted array; to minimize it, choose the smallest possible, which is the element at index floor((k-1)/2).
Pro tip: Mention that the order of elements in a subsequence doesn't affect the median, so sorting is valid. Also, explicitly state the indices and handle edge cases like k=1 or k=n.
Confirm that a subsequence maintains relative order but the median depends only on the multiset of values. Ask about constraints (e.g., array size, k range) to determine if sorting is acceptable.
Sort the array in non-decreasing order. This allows us to easily select any k elements and reason about the median position.
For a sorted subsequence of length k, the median is at index m = floor((k-1)/2) (0-indexed). This is the lower median for even k.
To maximize the median, we want the largest possible value at position m. This is achieved by taking the m-th element from the end of the sorted array, i.e., sorted[n - k + m].
To minimize the median, we want the smallest possible value at position m. This is achieved by taking the m-th element from the start, i.e., sorted[m].
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.