The k Strongest Values in an Array - Problem

You're given an array of integers arr and an integer k. Your task is to find the k strongest values in the array based on a unique strength metric.

What makes a value "stronger"?

A value arr[i] is considered stronger than arr[j] if:

  1. |arr[i] - m| > |arr[j] - m| where m is the median of the array
  2. If the distances are equal (|arr[i] - m| == |arr[j] - m|), then the larger value wins (arr[i] > arr[j])

Finding the median: Sort the array and take the element at position ((n - 1) / 2) using 0-based indexing.

Example: For [6, -3, 7, 2, 11], sorted becomes [-3, 2, 6, 7, 11]. The median is at index (5-1)/2 = 2, which is 6.

Return the k strongest values in any order.

Input & Output

example_1.py โ€” Basic Case
$ Input: arr = [1, 2, 3, 4, 5], k = 2
โ€บ Output: [5, 1]
๐Ÿ’ก Note: Median is 3 (middle of sorted array). Strengths: |1-3|=2, |2-3|=1, |3-3|=0, |4-3|=1, |5-3|=2. Both 1 and 5 have strength 2, but we can return in any order.
example_2.py โ€” Original Example
$ Input: arr = [6, -3, 7, 2, 11], k = 3
โ€บ Output: [11, 7, 6]
๐Ÿ’ก Note: Sorted: [-3, 2, 6, 7, 11], median = 6. Strengths: |-3-6|=9, |2-6|=4, |6-6|=0, |7-6|=1, |11-6|=5. Top 3 strongest: -3(9), 11(5), 2(4). But since 11 > -3 in tie-breaking, result includes [11, 7, 6] or similar.
example_3.py โ€” Single Element
$ Input: arr = [1], k = 1
โ€บ Output: [1]
๐Ÿ’ก Note: Only one element, so it's automatically the strongest (and weakest). Median is 1, strength is |1-1|=0.

Constraints

  • 1 โ‰ค arr.length โ‰ค 105
  • -105 โ‰ค arr[i] โ‰ค 105
  • 1 โ‰ค k โ‰ค arr.length
  • The answer can be returned in any order

Visualization

Tap to expand
Finding the Strongest ValuesStep 1: Original Array6-37211Step 2: Sort to Find Median-326MEDIAN711Step 3: Calculate Strengths (Distance from Median)-3|โˆ’3โˆ’6|=92|2โˆ’6|=46|6โˆ’6|=07|7โˆ’6|=111|11โˆ’6|=5Ranking by Strength1st: -3 (9)2nd: 11 (5)3rd: 2 (4)Key Insight: Use Two Pointers on Sorted Array!Strongest values are at the extremes of sorted arrayLEFT pointerRIGHT pointer
Understanding the Visualization
1
Find the Median
Sort all athletes by performance and find the middle performer (median)
2
Calculate Deviations
Measure how far each athlete's performance is from the median
3
Rank by Extremeness
The athletes with the largest deviations (positive or negative) are 'strongest'
4
Break Ties by Performance
If two athletes have the same deviation, the higher performer wins
Key Takeaway
๐ŸŽฏ Key Insight: After sorting, the strongest values will always be at the extremes (left and right ends) since they have the maximum distance from the median. Two pointers efficiently capture this without needing to calculate all strengths explicitly!
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
28.5K Views
Medium Frequency
~15 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen