Given an array A[] of n odd integers and an integer X. Calculate the minimum number of operations required to make the median of the array equal to X, where, in one operation we can either increase or decrease any single element by one.
Examples:
Input: A[] = {6, 5, 8}, X = 8
Output: 2
Explanation:
Here 6 can be increased twice. The array will become 8, 5, 8, which becomes 5, 8, 8 after sorting, hence the median is equal to 8.
Input: A[] = {1, 4, 7, 12, 3, 5, 9}, X = 5
Output: 0
Explanation:
After sorting 5 is in middle position hence 0 steps are required.
Approach: The idea for changing the median of the array will be to sort the given array. Then after sorting, the best possible candidate for making the median is the middle element because it will be better to reduce the numbers before the middle element as they are smaller and increase the numbers after the middle element as they are larger.
Below is the implementation of the above approach:
C++
// C++ implementation to determine the // Minimum numbers of steps to make // median of an array equal X #include <bits/stdc++.h> using namespace std; // Function to count minimum // required operations to // make median X int count(vector<int> a, int X) { // Sorting the array a[] sort(a.begin(), a.end()); int ans = 0; // Calculate the size of array int n = a.size(); // Iterate over the array for (int i = 0; i < n; i++) { // For all elements // less than median if (i < n / 2) ans += max(0, a[i] - X); // For element equal // to median else if (i == n / 2) ans += abs(X - a[i]); // For all elements // greater than median else ans += max(0, X - a[i]); } // Return the answer return ans; } // Driver code int main() { vector<int> a = { 6, 5, 8 }; int X = 8; cout << count(a, X) << "\n"; return 0; } |
Java
// Java implementation to determine the // Minimum numbers of steps to make // median of an array equal X import java.util.*; class GFG{ // Function to count minimum // required operations to // make median X static int count(int[] a, int X) { // Sorting the array a[] Arrays.sort(a); int ans = 0; // Calculate the size of array int n = a.length; // Iterate over the array for(int i = 0; i < n; i++) { // For all elements // less than median if (i < n / 2) ans += Math.max(0, a[i] - X); // For element equal // to median else if (i == n / 2) ans += Math.abs(X - a[i]); // For all elements // greater than median else ans += Math.max(0, X - a[i]); } // Return the answer return ans; } // Driver code public static void main(String[] args) { int []a = { 6, 5, 8 }; int X = 8; System.out.print(count(a, X) + "\n"); } } // This code is contributed by Amit Katiyar |
Python3
# Python3 implementation to determine the # Minimum numbers of steps to make # median of an array equal X # Function to count minimum # required operations to # make median X def count(a, X): # Sorting the array a[] a.sort() ans = 0 # Calculate the size of array n = len(a) # Iterate over the array for i in range(n): # For all elements # less than median if (i < n // 2): ans += max(0, a[i] - X) # For element equal # to median elif (i == n // 2): ans += abs(X - a[i]) # For all elements # greater than median else: ans += max(0, X - a[i]); # Return the answer return ans # Driver code a = [ 6, 5, 8 ] X = 8 print(count(a, X)) # This code is contributed by divyeshrabadiya07 |
C#
// C# implementation to determine the // Minimum numbers of steps to make // median of an array equal X using System; class GFG{ // Function to count minimum // required operations to // make median X static int count(int[] a, int X) { // Sorting the array []a Array.Sort(a); int ans = 0; // Calculate the size of array int n = a.Length; // Iterate over the array for(int i = 0; i < n; i++) { // For all elements // less than median if (i < n / 2) ans += Math.Max(0, a[i] - X); // For element equal // to median else if (i == n / 2) ans += Math.Abs(X - a[i]); // For all elements // greater than median else ans += Math.Max(0, X - a[i]); } // Return the answer return ans; } // Driver code public static void Main(String[] args) { int []a = { 6, 5, 8 }; int X = 8; Console.Write(count(a, X) + "\n"); } } // This code is contributed by Amit Katiyar |
2
Time Complexity: O(N * log N)
Auxiliary Space Complexity: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Minimum operations required to make all Array elements divisible by K
- Minimum no. of operations required to make all Array Elements Zero
- Minimum operations required to make all the array elements equal
- Find the minimum number of operations required to make all array elements equal
- Minimum operations required to make every element greater than or equal to K
- Minimum increment or decrement operations required to make the array sorted
- Minimum number of operations required to make two strings equal
- Minimum pair merge operations required to make Array non-increasing
- Minimum number of elements to add to make median equals x
- Find the number of operations required to make all array elements Equal
- Minimize operations required to make each element of Array equal to it's index value
- Count of minimum reductions required to get the required sum K
- Minimum operations required to remove an array
- Minimum operations required to transform a sequence of numbers to a sequence where a[i]=a[i+2]
- Minimum number of operations required to reduce N to 1
- Minimum number of operations required to delete all elements of the array
- Minimum operations required to modify the array such that parity of adjacent elements is different
- Minimum number of given operations required to convert a permutation into an identity permutation
- Minimum operations required to change the array such that |arr[i] - M| <= 1
- Minimum number of given operations required to reduce the array to 0 element
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

