Average of remaining elements after removing K largest and K smallest elements from array | Java implementation of the above approach ; Function to find average ; base case if 2 * k >= n means all element get removed ; first sort all elements ; sum of req number ; find average ; Driver code
import java . io . * ; import java . util . * ; class GFG { static double average ( int arr [ ] , int n , int k ) { double total = 0 ; if ( 2 * k >= n ) return 0 ; Arrays . sort ( arr ) ; int start = k , end = n - k - 1 ; for ( int i = start ; i <= end ; i ++ ) total += arr [ i ] ; return ( total / ( n - 2 * k ) ) ; } 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 ) ) ; } }