↓Skip to main content
  1. LeetCode/

LeetCode 1471: The k Strongest Values in an Array

·2 mins· ·
LeetCode Medium Two-Pointers Sorting Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-01-25

  • Submissions: Wrong Answer → Accepted

Approach
#

Sort and take the median m = arr[(n - 1) // 2]. Strength is |arr[i] - m|, with ties going to the larger value.

After sorting, the value farthest from m is always at one of the ends. So use converging pointers and compare the two ends:

  • m - arr[left] > arr[right] - m: the left end is stronger; take it.
  • Otherwise take the right end (on equal distance the right value is larger, so it is stronger).

Taking k values gives the answer.

My Attempts
#

Attempt 1 used arr[n // 2] as the median, but the problem defines it as arr[(n - 1) // 2]; they differ by one for even lengths. For arr = [2, -5], k = 1, sorting gives [-5, 2], the median is -5, and the strongest is 2; using 2 as the median returned [-5].

Solution
#

class Solution:
    def getStrongest(self, arr: List[int], k: int) -> List[int]:
        arr.sort()
        m = arr[(len(arr) - 1) // 2]
        left, right = 0, len(arr) - 1
        res = []
        while len(res) < k:
            if m - arr[left] > arr[right] - m:
                res.append(arr[left])
                left += 1
            else:
                res.append(arr[right])            # on a tie, the right value is larger
                right -= 1
        return res

My original looped while left < right and appended the last element separately when k == n; looping on len(res) < k avoids that, since both branches take the same element when left == right.

Complexity
#

Time O(n log n), space O(1) apart from the output and sorting.

Mistakes to Avoid
#

  • Using the wrong median index: it is (n - 1) // 2.
  • On equal distance, take the larger value, which is the right end.

Interview Explanation
#

After sorting I find the median. Strength is distance from the median, so the strongest value is always at one end. Converging pointers compare the ends: take the left one only if its distance is strictly larger, otherwise the right one (ties favor the larger value), k times. Time O(n log n).

Related

LeetCode 2563: Count the Number of Fair Pairs
·3 mins
LeetCode Medium Two-Pointers Sorting Binary-Search Array
LeetCode 2563 note: split “sum in [lower, upper]” into “sum <= upper” minus “sum <= lower - 1”, and count each with converging pointers over the sorted array in O(n)
LeetCode 1498: Number of Subsequences That Satisfy the Given Sum Condition
·2 mins
LeetCode Medium Two-Pointers Sorting Binary-Search Array
LeetCode 1498 note: only the minimum and maximum matter, so order does not and we can sort; fixing nums[left] as the minimum, the elements between are free, giving 2^(right - left) choices
LeetCode 923: 3Sum With Multiplicity
·4 mins
LeetCode Medium Two-Pointers Sorting Hash-Table Counting Array
LeetCode 923 note: sort, fix the first number, and find the other two with converging pointers; equal end values are counted with a combination, different ones by multiplying each end’s duplicate count
LeetCode 948: Bag of Tokens
·2 mins
LeetCode Medium Two-Pointers Greedy Sorting Array
LeetCode 948 note: after sorting, buy score with the cheapest token and buy power with the most expensive one; trade for power only when score > 0, and stop when neither move works
LeetCode 825: Friends Of Appropriate Ages
·3 mins
LeetCode Medium Two-Pointers Sorting Binary-Search Array
LeetCode 825 note: after sorting, the people someone can invite have ages in (0.5 × age + 7, age], a contiguous range kept with two same-direction pointers, minus the person themself
LeetCode 75: Sort Colors
·2 mins
LeetCode Medium Array Two-Pointers Sorting Quicksort
LeetCode 75 note: the Dutch flag, with 0s left of low and 2s right of high; after swapping a 2 to high, do not advance, since the swapped-in value is unchecked