Description
You are given an integer array nums and an integer k. In one operation, you can increase or decrease any element by 1. Return the minimum number of operations to make the median of nums equal to k.
Examples
Input:
nums = [2,5,6,8,5], k = 4Output:
2Explanation:
Change 5->4 and 5->4, median becomes 4.
Input:
nums = [1,2,3,4,5], k = 10Output:
5Explanation:
The sorted array [1,2,3,4,5] has median 3 at index 2. To change the median to the target value requires 5 increment/decrement operations total.
Input:
nums = [10,20,30], k = 15Output:
5Explanation:
With 3 elements, the median is the middle element after sorting: [10,20,30]. The median is 20 at index 1. To make the median equal to 15, the task requires change 20→15, which requires |20-15| = 5 operations.
Constraints
- •
1 ≤ nums.length ≤ 2 * 10^5 - •
1 ≤ nums[i] ≤ 10^9