Mean of range in array | Java program to find floor value of mean in range l to r ; To find mean of range in l to r ; Both sum and count are initialize to 0 ; To calculate sum and number of elements in range l to r ; Calculate floor value of mean ; Returns mean of array in range l to r ; Driver program to test findMean ( )
public class Main { static int findMean ( int arr [ ] , int l , int r ) { int sum = 0 , count = 0 ; for ( int i = l ; i <= r ; i ++ ) { sum += arr [ i ] ; count ++ ; } int mean = ( int ) Math . floor ( sum / count ) ; return mean ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; System . out . println ( findMean ( arr , 0 , 2 ) ) ; System . out . println ( findMean ( arr , 1 , 3 ) ) ; System . out . println ( findMean ( arr , 0 , 4 ) ) ; } }