Count of elements whose absolute difference with the sum of all the other elements is greater than k | Function to return the number of anomalies ; To store the count of anomalies ; To store the sum of the array elements ; Find the sum of the array elements ; Count the anomalies ; Driver code
function countAnomalies ( arr , n , k ) { var cnt = 0 ; var i , sum = 0 ; for ( i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; for ( i = 0 ; i < n ; i ++ ) if ( Math . abs ( arr [ i ] - ( sum - arr [ i ] ) ) > k ) cnt ++ ; return cnt ; } var arr = [ 1 , 3 , 5 ] ; var n = arr . length ; var k = 1 ; document . write ( countAnomalies ( arr , n , k ) ) ;