Make all elements of an array equal with the given operation | Function that returns true if all the elements of the array can be made equal with the given operation ; To store the sum of the array elements and the maximum element from the array ; driver program
function isPossible ( n , k , arr ) { let sum = arr [ 0 ] ; let maxVal = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { sum += arr [ i ] ; maxVal = Math . max ( maxVal , arr [ i ] ) ; } if ( maxVal > ( sum + k ) / n ) return false ; return true ; } let k = 8 ; let arr = [ 1 , 2 , 3 , 4 ] ; let n = arr . length ; if ( isPossible ( n , k , arr ) ) document . write ( " " ) ; else document . write ( " " ) ;