Minimum Operations to Make Median of Array Equal to K - Problem
Transform Array to Target Median
You're given an integer array
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
Return the minimum operations needed to achieve this transformation.
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
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
โ Linear Growth
Space Complexity
O(n)
Recursion stack depth and array storage
โก 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code