Count of elements that can be deleted without disturbing the mean of the initial array | Function to find the elements which do not change the mean on removal ; To store the sum of the array elements ; To store the initial mean ; to store the count of required elements ; Iterate over the array ; Finding the new mean ; If the new mean equals to the initial mean ; Driver code
function countElements ( arr , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; let mean = sum / n ; let cnt = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let newMean = ( sum - arr [ i ] ) / ( n - 1 ) ; if ( newMean == mean ) cnt ++ ; } return cnt ; } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let n = arr . length ; document . write ( countElements ( arr , n ) ) ;