Given an array of integers arr and an integer k.
A value arr[i] is said to be 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 said to be stronger than arr[j] if arr[i] > arr[j].
Return a list of the strongest k values in the array. return the answer in any arbitrary order.
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 in position ((n - 1) / 2) in the sorted list (0-indexed).
arr = [6, -3, 7, 2, 11], n = 5 and the centre is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the centre is arr[m] where m = ((5 - 1) / 2) = 2. The centre is 6.arr = [-7, 22, 17, 3], n = 4 and the centre is obtained by sorting the array arr = [-7, 3, 17, 22] and the centre is arr[m] where m = ((4 - 1) / 2) = 1. The centre is 3.Example 1:
Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Centre is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1.
Example 2:
Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Centre is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5].
Example 3:
Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Centre is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted.
Constraints:
1 <= arr.length <= 105-105 <= arr[i] <= 1051 <= k <= arr.lengthWhen you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To find the 'strongest' values, we'll check every single number in the list one by one. We'll then compare each of them to a special value, and see how 'strong' they are. We will then simply pick the top k 'strongest' numbers according to this criteria.
Here's how the algorithm would work step-by-step:
def get_k_strongest_values(
numbers, k):
middle_value = sorted(numbers)[(len(numbers) - 1) // 2]
# Calculate the strength of each number based on its distance from the median
strength_values = []
for number in numbers:
strength = abs(number - middle_value)
strength_values.append((number, strength))
# Sort the numbers by their strength in descending order
strongest_values = sorted(
strength_values,
key=lambda item: item[1],
reverse=True
)
# Extract the k strongest numbers
result = [number for number, _ in strongest_values[:k]]
return resultTo find the strongest values efficiently, we first need to know the middle point of the values. Then, we can compare each value to that middle point and pick out the ones that are farthest away, because those are considered the strongest.
Here's how the algorithm would work step-by-step:
def get_strongest(array, k):
array.sort()
array_length = len(array)
median = array[(array_length - 1) // 2]
# Sort by strength, then original order.
sorted_by_strength = sorted(array, key=lambda number:
(abs(number - median), array.index(number,0,len(array))),reverse=True)
# Return the first k strongest values
return sorted_by_strength[:k]| Case | How to Handle |
|---|---|
| Null or empty input array | Return an empty list or throw an IllegalArgumentException, depending on the specific requirements of the problem and coding style. |
| Array with fewer than k elements | Return the array sorted by strength value, or truncate to the length of the array if k exceeds the array length. |
| Array with all identical values | The median will be the repeated value, and the k strongest values will be the first k elements when sorted by index (or any k elements). |
| Large array size impacting sorting performance | Consider using a more efficient sorting algorithm like quicksort or mergesort if the naive approach of sorting takes too long (O(n log n) or better) on large arrays. |
| Integer overflow when calculating strength (|arr[i] - median|) | Use long data type to prevent overflow during the subtraction and absolute value calculation. |
| k is zero or negative | Return an empty list or throw an IllegalArgumentException, since there is no valid set of strongest values. |
| Array contains duplicate strongest values. | The problem definition should clarify how to handle ties in strength when selecting the top k elements; select any k values that satisfy the strongest criteria. |
| Array contains negative numbers, zeros, and positive numbers. | The absolute value calculation ensures that strength is always a non-negative value, so the algorithm works correctly with mixed positive, negative, and zero values. |