Untitled

mail@pastecode.io avatar
unknown
python
2 months ago
680 B
2
Indexable
Never
class Solution:
    def minOperationsToMakeMedianK(self, nums: List[int], k: int) -> int:
        ans = 0
        mid = len(nums)//2
        nums.sort()
        
        if nums[mid] == k:
            return 0
        
        if nums[mid] < k:
            for i in range(mid, len(nums)):
                if nums[i] >= k:
                    break
                    
                ans += abs(k - nums[i])
                
            return ans
        
        if nums[mid] > k:
            i = mid
            while i >= 0 and nums[i] >= k:
                ans += abs(k - nums[i])
                i -= 1
                
            return ans
                
            
Leave a Comment