Minimum Operations to Make Median of Array Equal to K - Problem
Transform Array to Target Median

You're given an integer array nums and a target value k. Your goal is to make the median of the array equal to k using the minimum number of operations.

In each operation, you can increase or decrease any element by exactly 1.

What's the median?
The median is the middle element when the array is sorted. For arrays with even length, we take the larger of the two middle elements.

Example: In array [1, 3, 6, 2, 7, 9], after sorting we get [1, 2, 3, 6, 7, 9]. The median is 6 (larger of the two middle elements 3 and 6).

Return the minimum operations needed to achieve this transformation.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [2,5,6,9,1], k = 5
โ€บ Output: 4
๐Ÿ’ก Note: After sorting: [1,2,5,6,9]. Median is 5 (index 2). To make median = 5: already 5, but we need 9 โ†’ 5 (4 operations). Total: 4 operations.
example_2.py โ€” Even Length Array
$ Input: nums = [1,2,3,4], k = 4
โ€บ Output: 0
๐Ÿ’ก Note: After sorting: [1,2,3,4]. For even length, median is larger middle element = 3. To make median = 4: change 3 โ†’ 4 (1 operation), but 4 becomes new median and it's already 4. Actually, median is at index n//2 = 2, which is 3. Need 1 operation to change 3โ†’4.
example_3.py โ€” All Same Elements
$ Input: nums = [7,7,7,7,7], k = 1
โ€บ Output: 18
๐Ÿ’ก Note: Median is 7 (middle element). Need to change median to 1 (6 operations). Also need to ensure elements to right of median are โ‰ฅ 1, but they're all 7 which is fine. Only median needs changing: 6 operations. Wait, let me recalculate: median at index 2 needs 7โ†’1 (6 ops), elements at indices 3,4 need to be โ‰ฅ1 but are 7 (fine), elements at indices 0,1 need to be โ‰ค1, so 7โ†’1 each (6+6=12 ops). Total: 6+12=18.

Visualization

Tap to expand
Student Height Line AnalogyMaking the middle student have target height k๐Ÿ˜Šh=1๐Ÿ˜Šh=2๐Ÿ˜Šh=5โ†’k=3MEDIAN๐Ÿ˜Šh=6โ†’3๐Ÿ˜Šh=9โ†’3Ground LevelGreedy Strategy Explanation1. Students to left of median (heights 1,2) โ‰ค k=3: No adjustment needed2. Median student (height 5) โ†’ shrink to k=3: 2 operations3. Students to right (heights 6,9): both > k, shrink to k=3: 3+6=9 operations
Understanding the Visualization
1
Line up students by height
Sort the array to arrange students from shortest to tallest
2
Find the middle position
Locate the median position (middle student in the line)
3
Adjust left side students
Students shorter than median who are taller than k need to shrink
4
Adjust right side students
Students taller than median who are shorter than k need to grow
5
Adjust the middle student
The median student grows or shrinks to exactly height k
Key Takeaway
๐ŸŽฏ Key Insight: After sorting, we only adjust elements that 'block' the median from reaching target k - left elements that are too big, right elements that are too small, and the median itself.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(3^n)

For each element, we can increase, decrease, or keep same, leading to 3^n combinations

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Recursion stack depth and array storage

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • 1 โ‰ค nums[i] โ‰ค 109
  • 1 โ‰ค k โ‰ค 109
  • The median follows the larger middle element rule for even-length arrays
Asked in
Google 35 Amazon 28 Microsoft 22 Meta 18
43.6K Views
Medium Frequency
~18 min Avg. Time
1.8K 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