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.
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
- 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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find last two remaining elements after removing median of any 3 consecutive elements repeatedly
- Rearrange an array in order - smallest, largest, 2nd smallest, 2nd largest, ..
- Maximize sum of remaining elements after every removal of the array half with greater sum
- Smallest prefix to be deleted such that remaining array can be rearranged to form a sorted array
- K-th smallest element after removing some integers from natural numbers
- K-th smallest element after removing given integers from natural numbers | Set 2
- Last element remaining by deleting two largest elements and replacing by their absolute difference if they are unequal
- Find the last remaining element after repeated removal of odd and even indexed elements alternately
- Find last remaining element after reducing the Array
- Length of smallest subarray to be removed such that the remaining array is sorted
- Length of smallest subarray to be removed to make sum of remaining elements divisible by K
- Maximize the sum of differences of consecutive elements after removing exactly K elements
- Minimize the sum of differences of consecutive elements after removing exactly K elements
- Maximum distinct elements after removing k elements
- Find the sum of remaining sticks after each iterations
- Maximum length of Strictly Increasing Sub-array after removing at most one element
- Minimum number of distinct elements after removing m items
- Minimum number of distinct elements after removing M items | Set 2
- Maximize Bitwise AND of first element with complement of remaining elements for any permutation of given 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
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.