Average of remaining elements after removing K largest and K smallest elements from array

Given an array of N integers. The task is to find the average of the numbers after removing k largest elements and k smallest element from the array i.e. calculate the average value of the remaining N – 2K elements.
Examples:Â
Input: arr = [1, 2, 4, 4, 5, 6], K = 2
Output: 4
Remove 2 smallest elements i.e. 1 and 2
Remove 2 largest elements i.e. 5 and 6
Remaining elements are 4, 4. So average of 4, 4 is 4.Input: arr = [1, 2, 3], K = 3
Output: 0
Approach:Â Â
- If no. of elements to be removed is greater than no. of elements present in the array, then ans = 0.
- Else, Sort all the elements of the array. Then, calculate average of elements from Kth index to n-k-1th index.
Below is the implementation of the above approach:Â Â
C++
// C++ implementation of the above approach#include <bits/stdc++.h>using namespace std;Â
// Function to find averagedouble average(int arr[], int n, int k){Â Â Â Â double total = 0;Â
    // base case if 2*k>=n    // means all element get removed    if (2 * k >= n)        return 0;Â
    // first sort all elements    sort(arr, arr + n);    int start = k, end = n - k - 1;Â
    // sum of req number    for (int i = start; i <= end; i++)        total += arr[i];Â
    // find average    return (total / (n - 2 * k));}Â
// Driver codeint main(){Â Â Â Â int arr[] = { 1, 2, 4, 4, 5, 6 };Â Â Â Â int n = sizeof(arr) / sizeof(arr[0]);Â Â Â Â int k = 2;Â
    cout << average(arr, n, k) << endl;Â
    return 0;} |
Java
// Java implementation of the above approachÂ
import java.io.*;import java.util.*;class GFG {Â
// Function to find averagestatic double average(int arr[], int n, int k){Â Â Â Â double total = 0;Â
    // base case if 2*k>=n    // means all element get removed    if (2 * k >= n)        return 0;Â
    // first sort all elements    Arrays.sort(arr);    int start = k, end = n - k - 1;Â
    // sum of req number    for (int i = start; i <= end; i++)        total += arr[i];Â
    // find average    return (total / (n - 2 * k));}Â
// Driver codeÂ
Â
    public static void main (String[] args) {            int arr[] = { 1, 2, 4, 4, 5, 6 };    int n = arr.length;    int k = 2;Â
    System.out.println( average(arr, n, k));     }}// This code is contributed by anuj_67.. |
Python3
# Python3 implementation of the # above approach Â
# Function to find average def average(arr, n, k) :Â Â Â Â total = 0Â
    # base case if 2*k>=n     # means all element get removed     if (2 * k >= n) :        return 0Â
    # first sort all elements     arr.sort()         start , end = k , n - k - 1Â
    # sum of req number     for i in range(start, end + 1) :        total += arr[i] Â
    # find average     return (total / (n - 2 * k))Â
# Driver code if __name__ == "__main__" : Â
    arr = [ 1, 2, 4, 4, 5, 6 ]     n = len(arr)    k = 2Â
    print(average(arr, n, k)) Â
# This code is contributed by Ryuga |
C#
// C# implementation of the above approachÂ
using System; public class GFG {      // Function to find average    static double average(int []arr, int n, int k)    {        double total = 0;Â
        // base case if 2*k>=n        // means all element get removed        if (2 * k >= n)            return 0;Â
        // first sort all elements        Array.Sort(arr);        int start = k, end = n - k - 1;Â
        // sum of req number        for (int i = start; i <= end; i++)            total += arr[i];Â
        // find average        return (total / (n - 2 * k));    }Â
    // Driver codeÂ
Â
        public static void Main() {                int []arr = { 1, 2, 4, 4, 5, 6 };        int n = arr.Length;        int k = 2;Â
        Console.WriteLine( average(arr, n, k));Â
    }}//This code is contributed by 29AjayKumar |
PHP
<?php// Php implementation of the // above approach Â
// Function to find average function average($arr, $n, $k){Â Â Â Â $total = 0; Â
    // base case if 2*k>=n     // means all element get removed     if (2 * $k >= $n)         return 0; Â
    // first sort all elements     sort($arr) ;         $start = $k ;    $end = $n - $k - 1; Â
    // sum of req number     for ($i = $start; $i <= $end; $i++)         $total += $arr[$i]; Â
    // find average     return ($total / ($n - 2 * $k)); } Â
// Driver code $arr = array(1, 2, 4, 4, 5, 6); $n = sizeof($arr);$k = 2; Â
echo average($arr, $n, $k);Â
// This code is contributed by Ryuga?> |
Javascript
<script>Â
// Javascript implementation of the above approachÂ
// Function to find averagefunction average(arr, n, k) {    var total = 0;         // Base case if 2*k>=n    // means all element get removed    if (2 * k >= n)        return 0;Â
    // First sort all elements    arr.sort();    var start = k, end = n - k - 1;Â
    // Sum of req number    for(i = start; i <= end; i++)        total += arr[i];Â
    // Find average    return (total / (n - 2 * k));}Â
// Driver codevar arr = [ 1, 2, 4, 4, 5, 6 ];var n = arr.length;var k = 2;Â
document.write(average(arr, n, k));Â
// This code is contributed by aashish1995Â
</script> |
Output
4
Time Complexity: O(n log n)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!


