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 average 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 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 code int 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; } |
chevron_right
filter_none
Java
// Java implementation of the above approach import java.io.*; import java.util.*; 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 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.. |
chevron_right
filter_none
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 |
chevron_right
filter_none
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 |
chevron_right
filter_none
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 ?> |
chevron_right
filter_none
Output:
4
Recommended Posts:
- k largest(or smallest) elements in an array | added Min Heap method
- Minimum number of elements to be removed such that the sum of the remaining elements is equal to k
- Check if the array has an element which is equal to sum of all the remaining elements
- Check if the array has an element which is equal to product of remaining elements
- Number of ways to choose elements from the array such that their average is K
- Count occurrences of the average of array elements with a given number
- Minimize the sum calculated by repeatedly removing any two elements and inserting their sum to the Array
- Maximum distinct elements after removing k elements
- Find the smallest and second smallest elements in an array
- Smallest greater elements in whole array
- Count the number of sub-arrays such that the average of elements present in the sub-array is greater than that not present in the sub-array
- Find the largest three elements in an array
- k-th smallest absolute difference of two elements in an array
- Rearrange an array in order - smallest, largest, 2nd smallest, 2nd largest, ..
- Largest sub-array whose all elements are perfect squares
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



