Minimum increment/decrement operations required to make Median as X
Last Updated :
23 Apr, 2021
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++
#include <bits/stdc++.h>
using namespace std;
int count(vector< int > a, int X)
{
sort(a.begin(), a.end());
int ans = 0;
int n = a.size();
for ( int i = 0; i < n; i++) {
if (i < n / 2)
ans += max(0, a[i] - X);
else if (i == n / 2)
ans += abs (X - a[i]);
else
ans += max(0, X - a[i]);
}
return ans;
}
int main()
{
vector< int > a = { 6, 5, 8 };
int X = 8;
cout << count(a, X) << "\n" ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int count( int [] a, int X)
{
Arrays.sort(a);
int ans = 0 ;
int n = a.length;
for ( int i = 0 ; i < n; i++)
{
if (i < n / 2 )
ans += Math.max( 0 , a[i] - X);
else if (i == n / 2 )
ans += Math.abs(X - a[i]);
else
ans += Math.max( 0 , X - a[i]);
}
return ans;
}
public static void main(String[] args)
{
int []a = { 6 , 5 , 8 };
int X = 8 ;
System.out.print(count(a, X) + "\n" );
}
}
|
Python3
def count(a, X):
a.sort()
ans = 0
n = len (a)
for i in range (n):
if (i < n / / 2 ):
ans + = max ( 0 , a[i] - X)
elif (i = = n / / 2 ):
ans + = abs (X - a[i])
else :
ans + = max ( 0 , X - a[i]);
return ans
a = [ 6 , 5 , 8 ]
X = 8
print (count(a, X))
|
C#
using System;
class GFG{
static int count( int [] a, int X)
{
Array.Sort(a);
int ans = 0;
int n = a.Length;
for ( int i = 0; i < n; i++)
{
if (i < n / 2)
ans += Math.Max(0, a[i] - X);
else if (i == n / 2)
ans += Math.Abs(X - a[i]);
else
ans += Math.Max(0, X - a[i]);
}
return ans;
}
public static void Main(String[] args)
{
int []a = { 6, 5, 8 };
int X = 8;
Console.Write(count(a, X) + "\n" );
}
}
|
Javascript
<script>
function bblSort(arr)
{
for ( var i = 0; i < arr.length; i++)
{
for ( var j = 0; j < (arr.length - i - 1); j++)
{
if (arr[j] > arr[j+1])
{
var temp = arr[j]
arr[j] = arr[j + 1]
arr[j + 1] = temp
}
}
}
return (arr);
}
function count(a, X)
{
a = bblSort(a);
var ans = 0;
var n = a.length;
for (i = 0; i < n; i++)
{
if (i < parseInt(n / 2))
ans += Math.max(0, a[i] - X);
else if (i == parseInt(n / 2))
ans += Math.abs(X - a[i]);
else
ans += Math.max(0, X - a[i]);
}
return ans;
}
var a = [ 6, 5, 8 ];
var X = 8;
document.write(count(a, X));
</script>
|
Time Complexity: O(N * log N)
Auxiliary Space Complexity: O(1)
Share your thoughts in the comments
Please Login to comment...