
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Minimum Operations to Make Median of Array Equal to K
The problem "Minimum Operations to Make the Median of the Array Equal to K" is used to adjust the elements of an integer array so that its median becomes equal to a given value k. In one operation, you can increase or decrease any element by 1.
Problem Statement
The goal is to find the minimum number of such operations to make the median of the array equal to K. The median of an array is the middle element when the array is sorted in non-decreasing order. The larger one is considered the median if there are two middle elements.
Example Scenario 1
Input: nums = [7, 1, 4], k = 5
Output: 1
The Sorted array = [1, 4, 7], Median = 4, Operations = Increasing 4 to 5(1 operation), Total operations = 1.
Example Scenario 2
Input: nums = [10, 2, 8, 6, 4], k = 6
Output: 0
The Sorted array = [2, 4, 6, 8, 10], Median = 6. No operations needed as already the median is 6.
Example Scenario 3
Input: nums = [20, 5, 15, 10, 25], k = 18
Output: 3
The Sorted array = [5, 10, 15, 20, 25], Median = 15, Operations = Increasing 15 to 18(15(+1+1+1): 3 operations), Total operations = 3.
Example Scenario 4
Input: nums = [12, 3, 2, 9, 5, 11 ], k = 9
Output: 0
The Sorted array = [2, 3, 5, 9, 11, 12], Median = 9, no need of performing operations as it is already equal to median. Here 5, 9 are the median as 9 is the highest, it is median and equal to k.
Time Complexity
The time complexity for sorting an array of ( n ) elements is O(n/log n) and to calculate the median involves iterating through the array, which takes O(n) where n is the number of operations.
To solve this problem in various programming languages, use the following methods.
- Sorting and Binary Search
- Two-Pointer Technique
Sorting and Binary Search
Example
The following approach sorts the array to find the median and uses binary search to find the optimal value that minimizes the number of operations needed to make the median equal to k.
#include <iostream> #include <vector> #include <algorithm> int minOperationsToMedian(std::vector<int>& nums, int K) { std::sort(nums.begin(), nums.end()); int n = nums.size(); int medianIndex = n / 2; int operations = 0; if (nums[medianIndex] != K) { operations = std::abs(nums[medianIndex] - K); } return operations; } int main() { std::vector<int> nums = {20, 5, 15, 10, 25}; int K = 18; std::cout << "Minimum operations = " << minOperationsToMedian(nums, K) << std::endl; return 0; }
Output
Minimum operations = 3
import java.util.Arrays; public class MinOperationsToMedian { public static int minOperationsToMedian(int[] nums, int K) { Arrays.sort(nums); int n = nums.length; int medianIndex = n / 2; int operations = 0; if (nums[medianIndex] != K) { operations = Math.abs(nums[medianIndex] - K); } return operations; } public static void main(String[] args) { int[] nums = {20, 5, 15, 10, 25}; int K = 18; System.out.println("Minimum operations = " + minOperationsToMedian(nums, K)); } }
Output
Minimum operations = 3
def min_operations_to_median(nums, K): nums.sort() n = len(nums) median_index = n // 2 operations = 0 if nums[median_index] != K: operations = abs(nums[median_index] - K) return operations nums = [20, 5, 15, 10, 25] K = 18 print("Minimum operations = ", min_operations_to_median(nums, K))
Output
Minimum operations = 3
package main import ( "fmt" "math" "sort" ) func minOperationsToMedian(nums []int, K int) int { sort.Ints(nums) n := len(nums) medianIndex := n / 2 operations := 0 if nums[medianIndex] != K { operations = int(math.Abs(float64(nums[medianIndex] - K))) } return operations } func main() { nums := []int{20, 5, 15, 10, 25} K := 18 fmt.Println("Minimum operations =", minOperationsToMedian(nums, K)) }
Output
Minimum operations = 3
Two-Pointer Technique
Example
In this technique, we use two pointers to identify the median position in the sorted array and adjust elements on either side of the median to make the median equal to k.
#include <bits/stdc++.h> using namespace std; int minOperationsToMakeMedianK(vector<int>& nums, int k) { sort(nums.begin(), nums.end()); int n = nums.size(); int operations = 0; for (int i = 0; i < n / 2; ++i) { operations += max(0, nums[i] - k); } for (int i = n / 2; i < n; ++i) { operations += max(0, k - nums[i]); } return operations; } int main() { vector<int> nums = {20, 5, 15, 10, 25}; int k = 18; cout <<"Minimum operations = " << minOperationsToMakeMedianK(nums, k) << endl; return 0; }
Output
Minimum operations = 3
import java.util.Arrays; public class Main { public static int minOperationsToMakeMedianK(int[] nums, int k) { Arrays.sort(nums); int n = nums.length; int operations = 0; for (int i = 0; i < n / 2; ++i) { operations += Math.max(0, nums[i] - k); } for (int i = n / 2; i < n; ++i) { operations += Math.max(0, k - nums[i]); } return operations; } public static void main(String[] args) { int[] nums = {20, 5, 15, 10, 25}; int k = 18; System.out.println("Minimum operations = " + minOperationsToMakeMedianK(nums, k)); } }
Output
Minimum operations = 3
import math def min_operations_to_make_median_k(nums, k): nums.sort() n = len(nums) operations = 0 for i in range(n // 2): operations += max(0, nums[i] - k) for i in range(n // 2, n): operations += max(0, k - nums[i]) return operations nums = [20, 5, 15, 10, 25] k = 18 print(f"Minimum operations = {min_operations_to_make_median_k(nums, k)}")
Output
Minimum operations = 3
package main import ( "fmt" "sort" ) func minOperationsToMakeMedianK(nums []int, k int) int { sort.Ints(nums) n := len(nums) operations := 0 for i := 0; i < n/2; i++ { operations += max(0, nums[i]-k) } for i := n / 2; i < n; i++ { operations += max(0, k-nums[i]) } return operations } func max(a, b int) int { if a > b { return a } return b } func main() { nums := []int{20, 5, 15, 10, 25} k := 18 fmt.Println("Minimum operations =", minOperationsToMakeMedianK(nums, k)) }
Output
Minimum operations = 3