📌  相关文章
📜  将K个元素添加到同一数组后,最大化给定数组的中位数

📅  最后修改于: 2021-04-29 10:14:03             🧑  作者: Mango

给定一个由N个元素组成的数组arr []和一个整数K ,其中K 。任务是将K个整数元素插入同一数组,以使结果数组的中位数最大化。打印最大化的中位数。

例子:

方法:为了使结果数组的中位数最大化,需要插入的所有元素必须大于数组中的最大元素。插入这些元素后,数组的新大小将为size = N + K。对数组进行排序,如果大小为奇数,否则数组的中位数将为arr [size / 2] (arr [(size / 2)– 1] + arr [size / 2])/ 2

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the maximized median
float getMaxMedian(int arr[], int n, int k)
{
    int size = n + k;
  
    // Sort the array
    sort(arr, arr + n);
  
    // If size is even
    if (size % 2 == 0) {
        float median = (float)(arr[(size / 2) - 1]
                               + arr[size / 2])
                       / 2;
        return median;
    }
  
    // If size is odd
    float median = arr[size / 2];
    return median;
}
  
// Driver code
int main()
{
    int arr[] = { 3, 2, 3, 4, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 2;
    cout << getMaxMedian(arr, n, k);
  
    return 0;
}


Java
import java.util.*;
  
// Java implementation of the approach
class GFG 
{
  
    // Function to return the maximized median
    static double getMaxMedian(int[] arr, int n, int k) 
    {
        int size = n + k;
  
        // Sort the array
        Arrays.sort(arr);
  
        // If size is even
        if (size % 2 == 0)
        {
            double median = (double) (arr[(size / 2) - 1]
                    + arr[size / 2])
                    / 2;
            return median;
        }
  
        // If size is odd
        double median1 = arr[size / 2];
        return median1;
    }
  
    // Driver code
    public static void main(String[] args) 
    {
        int[] arr = {3, 2, 3, 4, 2};
        int n = arr.length;
        int k = 2;
        System.out.print((int)getMaxMedian(arr, n, k));
  
    }
}
  
/* This code contributed by PrinciRaj1992 */


Python3
# Python 3 implementation of the approach
  
# Function to return the maximized median
def getMaxMedian(arr, n, k):
    size = n + k
  
    # Sort the array
    arr.sort(reverse = False)
  
    # If size is even
    if (size % 2 == 0):
        median = (arr[int(size / 2) - 1] + 
                  arr[int(size / 2)]) / 2
        return median
  
    # If size is odd
    median = arr[int(size / 2)]
    return median
  
# Driver code
if __name__ == '__main__':
    arr = [3, 2, 3, 4, 2]
    n = len(arr)
    k = 2
    print(getMaxMedian(arr, n, k))
  
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the approach
using System;
using System.Linq;
  
class GFG
{
      
// Function to return the maximized median
static double getMaxMedian(int []arr, int n, int k)
{
    int size = n + k;
  
    // Sort the array
    Array.Sort(arr);
  
    // If size is even
    if (size % 2 == 0)
    {
        double median = (double)(arr[(size / 2) - 1]
                            + arr[size / 2])
                    / 2;
        return median;
    }
  
    // If size is odd
    double median1 = arr[size / 2];
    return median1;
}
  
// Driver code
static void Main()
{
    int []arr = { 3, 2, 3, 4, 2 };
    int n = arr.Length;
    int k = 2;
    Console.WriteLine(getMaxMedian(arr, n, k));
}
}
  
// This code is contributed by mits


PHP


输出:
3