K-th largest deviation

Write codeMedium14 min

Your task

Given a list of integers, a target value, and an integer k (1 <= k <= length of the list), return the k-th largest absolute deviation from the target. The absolute deviation of an element x is |x - target|. Deviations from different elements may be equal; each element contributes one deviation and duplicates are counted separately. Example: nums = [100, 140, 90, 110], target = 120, k = 2 gives 20, the deviations are [20, 20, 30, 10], and arranged largest-first they read 30, 20, 20, 10.

Examples

kth_deviation(nums: list[int], target: int, k: int) -> int
Example 1
Inputkth_deviation([100, 140, 90, 110], 120, 2)
Output20