AmazonOA #43
Medians
Estimated EasyJudge-ready
Topics (AI-labelled)In-place ArraysGreedy
Choose any order-preserving subsequence of length k, sort it, and take its ⌈k/2⌉-th element as the median (for even lengths, the smaller middle value is taken, consistent with the source examples). Output the largest and the smallest median over all choices.
Input
The first line contains n k (1≤k≤n≤100000), and the second line contains n values (0..10⁹).
Output
Output the maximum median and the minimum median.
Example 1
Input:
3 2
1 2 3
Output:
2 1
Example 2
Input:
1 1
9
Output:
9 9
Example 3
Input:
5 3
1 2 3 4 5
Output:
4 2
Example notes
Example 1: the median is the smaller of the two chosen items; choosing [2,3] gives the maximum 2, and choosing [1,2] gives the minimum 1. Example 2: there is only 9, so both answers are 9. Example 3: the largest three items [3,4,5] have median 4, and the smallest three items [1,2,3] have median 2.
Editorial
Loading solution…