📌  相关文章
📜  使 Median 为 X 所需的最小增量/减量操作

📅  最后修改于: 2021-10-26 06:30:00             🧑  作者: Mango

给定一个由 n 个奇数组成的数组 A[]和一个整数 X 。计算使数组的中位数等于 X 所需的最少操作次数,其中,在一次操作中,我们可以将任何单个元素增加或减少 1。

例子:

方法:改变数组中位数的想法是对给定的数组进行排序。然后在排序之后,制作中位数的最佳候选者是中间元素,因为最好减少中间元素之前的数字,因为它们较小,而增加中间元素之后的数字,因为它们较大。

下面是上述方法的实现:

C++
// C++ implementation to determine the
// Minimum numbers of steps to make
// median of an array equal X
 
#include 
using namespace std;
 
// Function to count minimum
// required operations to
// make median X
int count(vector 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 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


Javascript


输出:
2

时间复杂度: O(N * log N)
辅助空间复杂度: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程