Count of elements whose absolute difference with the sum of all the other elements is greater than k Last Updated : 03 Mar, 2022 Comments Improve Suggest changes Like Article Like Report Given an array arr[] of N integers and an integer K, the task is to find the number of anomalies in the array. An anomaly is a number for which the absolute difference between it and all the other numbers in the array is greater than K. Find the number of anomalies. Examples: Input: arr[] = {1, 3, 5}, k = 1 Output: 2 1 and 3 are the anomalies. |1 - (3 + 5)| = 7 > 1 |3 - (1 + 5)| = 3 > 1 Input: arr[] = {7, 1, 8}, k = 5 Output: 1 Approach: Find the sum of all the array elements and store it in sum, now for every element of the array arr[i] if the absolute difference of arr[i] with sum - arr[i] is > k then it is an anomaly. Count all the anomalies in the array and print the result in the end. Below is the implementation of the above approach: C++ // C++ implementation of the approach #include<bits/stdc++.h> using namespace std; // Function to return the number of anomalies static int countAnomalies(int arr[], int n, int k) { // To store the count of anomalies int cnt = 0; // To store the sum of the array elements int i, sum = 0; // Find the sum of the array elements for (i = 0; i < n; i++) sum += arr[i]; // Count the anomalies for (i = 0; i < n; i++) if (abs(arr[i] - (sum - arr[i])) > k) cnt++; return cnt; } // Driver code int main() { int arr[] = { 1, 3, 5 }; int n = sizeof(arr) / sizeof(arr[0]); int k = 1; cout << countAnomalies(arr, n, k); } // This code is contributed // by Code_Mech Java // Java implementation of the approach class GFG { // Function to return the number of anomalies static int countAnomalies(int arr[], int n, int k) { // To store the count of anomalies int cnt = 0; // To store the sum of the array elements int i, sum = 0; // Find the sum of the array elements for (i = 0; i < n; i++) sum += arr[i]; // Count the anomalies for (i = 0; i < n; i++) if (Math.abs(arr[i] - (sum - arr[i])) > k) cnt++; return cnt; } // Driver code 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)); } } Python3 # Python3 implementation of the approach # Function to return the # number of anomalies def countAnomalies(arr, n, k): # To store the count of anomalies cnt = 0 # To store the Sum of # the array elements i, Sum = 0, 0 # Find the Sum of the array elements for i in range(n): Sum += arr[i] # Count the anomalies for i in range(n): if (abs(arr[i] - (Sum - arr[i])) > k): cnt += 1 return cnt # Driver code arr = [1, 3, 5] n = len(arr) k = 1 print(countAnomalies(arr, n, k)) # This code is contributed # by mohit kumar C# // C# implementation of the approach using System; class GFG { // Function to return the number of anomalies static int countAnomalies(int[] arr, int n, int k) { // To store the count of anomalies int cnt = 0; // To store the sum of the array elements int i, sum = 0; // Find the sum of the array elements for (i = 0; i < n; i++) sum += arr[i]; // Count the anomalies for (i = 0; i < n; i++) if (Math.Abs(arr[i] - (sum - arr[i])) > k) cnt++; return cnt; } // Driver code public static void Main() { int[] arr = { 1, 3, 5 }; int n = arr.Length; int k = 1; Console.WriteLine(countAnomalies(arr, n, k)); } } // This code is contributed by Code_Mech. PHP <?php // PHP implementation of the approach // Function to return // the number of anomalies function countAnomalies($arr, $n, $k) { // To store the count of anomalies $cnt = 0; // To store the sum of // the array elements $sum = 0; // Find the sum of the array elements for ($i = 0; $i < $n; $i++) $sum += $arr[$i]; // Count the anomalies for ($i = 0; $i < $n; $i++) if (abs($arr[$i] - ($sum - $arr[$i])) > $k) $cnt++; return $cnt; } // Driver code $arr = array(1, 3, 5); $n = count($arr); $k = 1; echo countAnomalies($arr, $n, $k); // This code is contributed by Ryuga ?> JavaScript <script> // Javascript implementation of the approach // Function to return the number of anomalies function countAnomalies(arr, n, k) { // To store the count of anomalies var cnt = 0; // To store the sum of the array elements var i, sum = 0; // Find the sum of the array elements for(i = 0; i < n; i++) sum += arr[i]; // Count the anomalies for(i = 0; i < n; i++) if (Math.abs(arr[i] - (sum - arr[i])) > k) cnt++; return cnt; } // Driver code var arr = [ 1, 3, 5 ]; var n = arr.length; var k = 1; document.write(countAnomalies(arr, n, k)); // This code is contributed by umadevi9616 </script> Output: 2 Time Complexity : O(n)Auxiliary Space : O(1) Comment More infoAdvertise with us Next Article Count elements whose sum with K is greater than max element S sanmoypal Follow Improve Article Tags : Mathematical DSA Arrays school-programming Practice Tags : ArraysMathematical Similar Reads Count elements in first Array with absolute difference greater than K with an element in second Array Given two arrays arr1[] and arr2[] and an integer K, our task is to find the number elements in the first array, for an element x, in arr1[], there exists at least one element y, in arr2[] such that absolute difference of x and y is greater than the integer K. Examples: Input: arr1 = {3, 1, 4}, arr2 7 min read Count elements whose sum with K is greater than max element Given an array arr[] and integer K, our task is to determine if the sum of each element in the array and K is greater than or equal to the maximum element that is present in the array that is arr[i] + k >= maxElement of array. Print the total count of all such elements. Examples: Input : arr = [2 7 min read Count maximum elements of an array whose absolute difference does not exceed K Given an array A and positive integer K. The task is to find maximum number of elements for which the absolute difference of any of the pair does not exceed K.Examples: Input: A[] = {1, 26, 17, 12, 15, 2}, K = 5 Output: 3 There are maximum 3 values so that the absolute difference of each pair does n 6 min read Find set of m-elements with difference of any two elements is divisible by k Given an array of n-positive integers and a positive integer k, find a set of exactly m-elements such that difference of any two element is equal to k. Examples : Input : arr[] = {4, 7, 10, 6, 9}, k = 3, m = 3 Output : Yes 4 7 10 Input : arr[] = {4, 7, 10, 6, 9}, k = 12, m = 4 Output : No Input : ar 7 min read Count of Array elements greater than all elements on its left and next K elements on its right Given an array arr[], the task is to print the number of elements which are greater than all the elements on its left as well as greater than the next K elements on its right. Examples: Input: arr[] = { 4, 2, 3, 6, 4, 3, 2}, K = 2 Output: 2 Explanation: arr[0](= 4): arr[0] is the 1st element in the 14 min read Count of Array elements greater than all elements on its left and next K elements on its right Given an array arr[], the task is to print the number of elements which are greater than all the elements on its left as well as greater than the next K elements on its right. Examples: Input: arr[] = { 4, 2, 3, 6, 4, 3, 2}, K = 2 Output: 2 Explanation: arr[0](= 4): arr[0] is the 1st element in the 14 min read Like