Dada una array arr[] y un entero K , la tarea es eliminar K % por ciento de los elementos de array de los elementos de array más pequeños y más grandes y calcular la media de la array restante.
Ejemplos:
Entrada: arr[] = {6, 2, 7, 5, 1, 2, 0, 3, 10, 2, 5, 0, 5, 5, 0, 8, 7, 6, 8, 0}, K = 5
Salida: 4.00000
Explicación:
Hay 20 elementos en la array. Por lo tanto, el 5% de 20 es 1 . Por lo tanto, se elimina 1 de los elementos más pequeños (es decir, 0) y 1 elemento de los elementos más grandes (es decir, 0) . Por lo tanto, la media de la array restante es 18 .Entrada: arr[] = {6, 0, 7, 0, 7, 5, 7, 8, 3, 4, 0, 7, 8, 1, 6, 8, 1, 1, 2, 4}, K = 10
Salida: 4.31250
Acercarse:
- Ordene la array arr[] .
- Encuentre el tamaño de la array .
- Calcule el K -ésimo por ciento del tamaño de la array.
- Ahora, agregue los elementos presentes en los índices K% a (N – 1) – K% .
- Finalmente, encuentre la media de esos elementos.
A continuación se muestra la implementación del enfoque anterior:
C++14
// C++ program for the above approach #include <bits/stdc++.h> 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.
Javascript
<script> // Javascript 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 function meanOfRemainingElements( arr, N, K) { // Sort the array arr.sort(function(a, b){return a-b}); // Find the K-th percent // of the array size let kthPercent = Math.floor((N * K) / 100); let sum = 0; // Traverse the array for (let 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 let mean = sum / (N - 2 * kthPercent); // Print mean upto 5 decimal places document.write(mean.toFixed(5)); } // Driver Code let arr = [ 6, 2, 7, 5, 1, 2, 0, 3, 10, 2, 5, 0, 5, 5, 0, 8, 7, 6, 8, 0 ]; let arr_size = arr.length; let K = 5; meanOfRemainingElements(arr, arr_size, K); </script>
4.00000
Complejidad de tiempo: O(N * logN)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por sayantanbose2001 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA