Count of elements whose absolute difference with the sum of all the other elements is greater than k | Java implementation of the approach ; 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
class GFG { static int countAnomalies ( int arr [ ] , int n , int k ) { int cnt = 0 ; int 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 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 5 } ; int n = arr . length ; int k = 1 ; System . out . print ( countAnomalies ( arr , n , k ) ) ; } }