Find the deleted value from the array when average of original elements is given | C ++ implementation of the approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMissing ( int arr [ ] , int n , int k , int avg ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } int num = ( avg * ( n + k ) ) - sum ; int den = k ; if ( num % den != 0 ) return -1 ; return ( num / den ) ; } int main ( ) { int k = 3 , avg = 4 ; int arr [ ] = { 2 , 7 , 3 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << findMissing ( arr , n , k , avg ) ; return 0 ; }