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).
arr = [1,2,3,4,5], k = 2[5,1]arr = [1,1,3,5,5], k = 2[5,5]Constraints
- 1 <= arr.length <= 10^5
- -10^5 <= arr[i] <= 10^5
- 1 <= k <= arr.length
Asked at 1 companies
Practice questions like this out loud in a mock coding interview with a live AI interviewer. First one free.