Number of anomalies in an array | A simple javascript solution to count anomalies in an array . ; Driver code
function countAnomalies ( arr , n , k ) { let res = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let j ; for ( j = 0 ; j < n ; j ++ ) if ( i != j && Math . abs ( arr [ i ] - arr [ j ] ) <= k ) break ; if ( j == n ) res ++ ; } return res ; } let arr = [ 7 , 1 , 8 ] ; let k = 5 ; let n = arr . length ; document . write ( countAnomalies ( arr , n , k ) ) ;