Question
Suppose you have an array A of n items, and you want to find the k items in A closest to the median of A
Suppose you have an array A of n items, and you want to find the k items in A closest to the median of A. For example, if A contains the 9 values {7,14,10,12,2,11,29,3,4} and k = 5, then the answer would be the values {7,14,10,12,11}, since the median is 10 and these are the five values in A closest to the value 10. Give an algorithm to solve this problem in O(n) time, and explain why your algorithm runs in O(n) time. You may use the selection algorithms discussed in the book and the notes as subroutines of your solution. (Hint: you may want to call the selection algorithm more than once.)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started