The k Strongest Values in an Array
Given an array of integers arr and an integer k, return a list of the strongest k values in the array. Return the answer in any arbitrary order.
A value arr[i] is stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m|, where m is the centre of the array.
If |arr[i] - m| == |arr[j] - m|, then arr[i] is stronger than arr[j] if arr[i] > arr[j].
The centre is the middle value in an ordered integer list. More formally, if the length of the list is n, the centre is the element at position ((n - 1) / 2) in the sorted list (0-indexed).
Example 1
Input
arr = [1,2,3,4,5], k = 2Output
[5,1]Centre is 3, and the strongest two elements are 5 and 1; [1, 5] would also be accepted.
Example 2
Input
arr = [1,1,3,5,5], k = 2Output
[5,5]Centre is 3, and the two values 5 and 5 are strongest.
Constraints
- 1 <= arr.length <= 10^5
- -10^5 <= arr[i] <= 10^5
- 1 <= k <= arr.length