Tap here to switch tabs
Problem
Submissions

Problem: Minimum Operations to Make Median of Array Equal to K

Minimum Operations to Make Median of Array Equal to K asks you to adjust array values by one per operation and return the fewest operations to make sorted array's middle or upper-middle value equal k.

Statement

You are given an integer array nums and a positive integer k.

In a single operation, you can increment or decrement any element of nums by 11.

Return the minimum number of operations required to make the median of nums equal to k.

Note: The median of an array is defined as the middle element after sorting the array in nondecreasing order. If the array contains an even number of elements, the median is the element located at index ⌊ext‘nums.length‘2⌋\lfloor \frac{ ext{`nums.length`}}{2} \rfloor using 00 indexing.

Constraints:

  • 1≤1 \leq nums.length ≤2imes105\leq 2 imes 10^5

  • 1≤1 \leq nums[i] ≤109\leq 10^9

  • 1≤1 \leq k ≤109\leq 10^9

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimum Operations to Make Median of Array Equal to K

Minimum Operations to Make Median of Array Equal to K asks you to adjust array values by one per operation and return the fewest operations to make sorted array's middle or upper-middle value equal k.

Statement

You are given an integer array nums and a positive integer k.

In a single operation, you can increment or decrement any element of nums by 11.

Return the minimum number of operations required to make the median of nums equal to k.

Note: The median of an array is defined as the middle element after sorting the array in nondecreasing order. If the array contains an even number of elements, the median is the element located at index ⌊ext‘nums.length‘2⌋\lfloor \frac{ ext{`nums.length`}}{2} \rfloor using 00 indexing.

Constraints:

  • 1≤1 \leq nums.length ≤2imes105\leq 2 imes 10^5

  • 1≤1 \leq nums[i] ≤109\leq 10^9

  • 1≤1 \leq k ≤109\leq 10^9