📌  相关文章
📜  除去K%的最小和最大数组元素后,给定数组的平均值

📅  最后修改于: 2021-05-14 00:30:04             🧑  作者: Mango

给定一个数组arr []和一个整数K ,任务是从最小最大的数组元素中删除百分之K的数组元素,并计算剩余数组的平均值

例子:

方法:

  1. 对数组arr []排序。
  2. 查找数组的大小。
  3. 计算数组大小的第K-
  4. 现在,将索引K%中存在的元素添加到(N – 1)– K%中
  5. 最后,找到这些元素的均值。

下面是上述方法的实现:

C++14
// C++ program for the above approach
#include 
using namespace std;
 
// Function to calculate the mean
// of a given array after removal
// of Kth percent of smallest and
// largest array elements
void meanOfRemainingElements(int arr[],
                             int N, int K)
{
    // Sort the array
    sort(arr, arr + N);
 
    // Find the K-th percent
    // of the array size
    int kthPercent = (N * K) / 100;
    float sum = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++)
 
        // Skip the first K-th
        // percent & last K-th
        // percent array elements
        if (i >= kthPercent && i < (N - kthPercent))
            sum += arr[i];
 
    // Mean of the rest of elements
    float mean = sum
                 / (N - 2 * kthPercent);
 
    // Print mean upto 5 decimal places
    cout << fixed << setprecision(5) << mean << endl;
}
 
// Driver Code
int main()
{
 
    int arr[] = { 6, 2, 7, 5, 1, 2, 0, 3, 10, 2,
                  5, 0, 5, 5, 0, 8, 7, 6, 8, 0 };
    int arr_size = sizeof(arr) / sizeof(arr[0]);
    int K = 5;
 
    meanOfRemainingElements(arr, arr_size, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
public class GFG
{
 
  // Function to calculate the mean
  // of a given array after removal
  // of Kth percent of smallest and
  // largest array elements
  static void meanOfRemainingElements(int[] arr, int N,
                                      int K)
  {
 
    // Sort the array
    Arrays.sort(arr);
 
    // Find the K-th percent
    // of the array size
    int kthPercent = (N * K) / 100;
    float sum = 0f;
 
    // Traverse the array
    for (int i = 0; i < N; i++)
 
      // Skip the first K-th
      // percent & last K-th
      // percent array elements
      if (i >= kthPercent && i < (N - kthPercent))
        sum += arr[i];
 
    // Mean of the rest of elements
    float mean = (sum / (N - 2 * kthPercent));
 
    // Print mean upto 5 decimal places
    System.out.format("%.5f", mean);
  }
 
 
  // Driver Code
  public static void main(String args[])
  {
    int[] arr = { 6, 2, 7, 5, 1, 2, 0, 3, 10, 2,
                 5, 0, 5, 5, 0, 8, 7, 6, 8,  0 };
    int arr_size = arr.length;
    int K = 5;
 
    meanOfRemainingElements(arr, arr_size, K);
  }
}
 
// This code is contributed by jana_sayantan.


Python3
# Python program for the above approach
 
# Function to calculate the mean
# of a given array after removal
# of Kth percent of smallest and
# largest array elements
def meanOfRemainingElements(arr, N, K):
   
    # Sort the array
    arr.sort()
 
    # Find the K-th percent
    # of the array size
    kthPercent = (N * K) / 100
    sum = 0
 
    # Traverse the array
    for i in range(N):
 
        # Skip the first K-th
        # percent & last K-th
        # percent array elements
        if (i >= kthPercent and i < (N - kthPercent)):
            sum += arr[i]
 
    # Mean of the rest of elements
    mean = sum/ (N - 2 * kthPercent)
 
    # Print mean upto 5 decimal places
    print( '%.5f'%mean)
 
# Driver Code
arr = [ 6, 2, 7, 5, 1, 2, 0, 3, 10, 2, 5, 0, 5, 5, 0, 8, 7, 6, 8, 0 ]
arr_size = len(arr)
K = 5
 
meanOfRemainingElements(arr, arr_size, K)
 
# This code is contributed by rohitsingh07052.


C#
// C# program for the above approach
using System;
 
class GFG {
 
    // Function to calculate the mean
    // of a given array after removal
    // of Kth percent of smallest and
    // largest array elements
    static void meanOfRemainingElements(int[] arr, int N,
                                        int K)
    {
       
        // Sort the array
        Array.Sort(arr);
 
        // Find the K-th percent
        // of the array size
        int kthPercent = (N * K) / 100;
        float sum = 0f;
 
        // Traverse the array
        for (int i = 0; i < N; i++)
 
            // Skip the first K-th
            // percent & last K-th
            // percent array elements
            if (i >= kthPercent && i < (N - kthPercent))
                sum += arr[i];
 
        // Mean of the rest of elements
        float mean = (sum / (N - 2 * kthPercent));
 
        // Print mean upto 5 decimal places
        Console.WriteLine(Math.Round(mean,5));
    }
 
    // Driver Code
    public static void Main()
    {
        int[] arr = { 6, 2, 7, 5, 1, 2, 0, 3, 10, 2,
                      5, 0, 5, 5, 0, 8, 7, 6, 8,  0 };
        int arr_size = arr.Length;
        int K = 5;
 
        meanOfRemainingElements(arr, arr_size, K);
    }
}
 
// This code is contributed by chitranayal.


输出:
4.00000

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