Skip to main content
Back to problems
#1471
Medium Algorithms

The k strongest values in an array

Array Two Pointers Sorting
62.6% acceptance
Feb 25, 2026
723
165
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).

Solution

Rust
Time O(n log n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn get_strongest(mut arr: Vec<i32>, k: i32) -> Vec<i32> {
    arr.sort_unstable();
    let n = arr.len();
    let m = arr[(n - 1) / 2];
    arr.sort_unstable_by(|&a, &b| {
      let da = (a - m).abs();
      let db = (b - m).abs();
      db.cmp(&da).then(b.cmp(&a))
    });
    arr.truncate(k as usize);
    arr
  }
}