📜  使Median为X所需的最小增/减操作

📅  最后修改于: 2021-05-07 08:17:05             🧑  作者: Mango

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

方法:更改数组中位数的想法是对给定数组进行排序。然后在排序之后,使中间值成为最佳的候选者是中间元素,因为减少中间元素之前的数字越小越好,而增加中间元素之后的数字越多越好。
下面是上述方法的实现:

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


输出:
2

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