Find the deleted value from the array when average of original elements is given | Function to return the missing element ; Find the sum of the array elements ; The numerator and the denominator of the equation ; If not divisible then X is not an integer it is a floating point number ; Return X ; Driver code
function findMissing ( arr , n , k , avg ) { var sum = 0 ; for ( var i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } var num = ( avg * ( n + k ) ) - sum ; var den = k ; if ( num % den != 0 ) return - 1 ; return ( Math . floor ( num / den ) ) ; } var k = 3 ; var avg = 4 ; var arr = [ 2 , 7 , 3 ] ; var n = arr . length ; document . write ( findMissing ( arr , n , k , avg ) ) ;