Count of elements whose absolute difference with the sum of all the other elements is greater than k
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 > 1Input: 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
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
[tabby title="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 = 1print(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 ?> |
2
Recommended Posts:
- Count maximum elements of an array whose absolute difference does not exceed K
- Count elements such that there are exactly X elements with values greater than or equal to X
- Count smaller elements on right side and greater elements on left side using Binary Index Tree
- k-th smallest absolute difference of two elements in an array
- Absolute Difference of even and odd indexed elements in an Array
- Absolute Difference of all pairwise consecutive elements in an array
- Minimum absolute difference of adjacent elements in a circular array
- Find maximum number of elements such that their absolute difference is less than or equal to 1
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Count subarrays with all elements greater than K
- Count the number of elements which are greater than any of element on right side of an array
- Noble integers in an array (count of greater elements is equal to value)
- Number of possible permutations when absolute difference between number of elements to the right and left are given
- Find all elements in array which have at-least two greater elements
- Find the difference of count of equal elements on the right and the left for each element
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



