Implement a function to find the K elements of a given array that are closet to the median. (Hint: You could modify Quick_Select to find the answer!)
- Request the user to enter a positive integer, and call it n.
- Generate n random integers between -100 to 100 and save them in a.
- Print the generated array.
- Request the user to enter a number between 1 to n, and call it K.
- Find the median of the array. (Hint:can you use quick select? What is the time complexity in this step?)
- Save the differences from the median (|a[i]-median|) in a new array and call it diff. (Note: The K closet elements/numbers have the K smallest difference from the median. What is the time complexity in this stage?)
- Use diff to find the K numbers. (Hint: can you use quick select again? What is the time complexity in this step?)
- Shift the found K numbers back to their original value (+median). (Question: What is the time complexity in this step?)
- Print the answer 😊
- Calculate the total time complexity of your algorithm and present your answer when demoing.
Reviews
There are no reviews yet.