<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. In one operation, you can increase or decrease any element by 1.</p>

<p>Return the <strong>minimum</strong> number of operations needed to make the <strong>median</strong> of <code>nums</code> <em>equal</em> to <code>k</code>.</p>

<p>The median of an array is defined as the middle element of the array when it is sorted in non-decreasing order. If there are two choices for a median, the larger of the two values is taken.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,5,6,8,5], k = 4</span></p>

<p><strong>Output:</strong> <span class="example-io">2</span></p>

<p><strong>Explanation:</strong></p>

<p>We can subtract one from <code>nums[1]</code> and <code>nums[4]</code> to obtain <code>[2, 4, 6, 8, 4]</code>. The median of the resulting array is equal to <code>k</code>.</p>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,5,6,8,5], k = 7</span></p>

<p><strong>Output:</strong> <span class="example-io">3</span></p>

<p><strong>Explanation:</strong></p>

<p>We can add one to <code>nums[1]</code> twice and add one to <code>nums[2]</code> once to obtain <code>[2, 7, 7, 8, 5]</code>.</p>
</div>

<p><strong class="example">Example 3:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,6], k = 4</span></p>

<p><strong>Output:</strong> <span class="example-io">0</span></p>

<p><strong>Explanation:</strong></p>

<p>The median of the array is already equal to <code>k</code>.</p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
	<li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def minOperationsToMakeMedianK(self, nums: List[int], k: int) -> int:
        nums.sort()
        # def get_median_and_index():
        def get_median():
            median_start = len(nums) // 2
            return nums[median_start]
            # if len(nums) % 2 != 0:
            #     return nums[median_start]
            
            # mid1 = nums[median_start]
            # mid2 = nums[median_start + 1]

            # return max(mid1, mid2)
        
        def is_greater(num: int) -> int:
            return num > k
        def is_lesser(num: int) -> int:
            return num < k
        
        def take_greater(nums: List[int]) -> List[int]:
            return list(filter(is_greater, nums))

        def take_lesser(nums: List[int]) -> List[int]:
            return list(filter(is_lesser, nums))
        
        def get_nums_needing_shift():
            """
            Returns median and all numbers before or after depending if median greater than k
            If median greater than k, we return all numbers greater (after) median
            Otherwise, we return all numbers less than median


            """
            median_start = len(nums) // 2
            median = get_median()

            # If odd, then there's a single choice for median
            if len(nums) % 2 != 0:
                if median < k:
                    return take_lesser(nums[median_start:])
                
                return take_greater(nums[:median_start+1])
            

            if median < k:
                start = median_start
                return take_lesser(nums[start:])
            
            end = median_start + 1
            return take_greater(nums[:end])
            


        
        median = get_median()

        if median == k:
            return 0

        # return get_nums_needing_shift()

        ops = 0
        for num in get_nums_needing_shift():
            ops += abs(num - k)
        return ops


37700