Count of elements whose absolute difference with the sum of all the other elements is greater than k
Last Updated :
03 Mar, 2022
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++
#include<bits/stdc++.h>
using namespace std;
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 ( abs (arr[i] -
(sum - arr[i])) > k)
cnt++;
return cnt;
}
int main()
{
int arr[] = { 1, 3, 5 };
int n = sizeof (arr) / sizeof (arr[0]);
int k = 1;
cout << countAnomalies(arr, n, k);
}
|
Java
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));
}
}
|
Python3
def countAnomalies(arr, n, k):
cnt = 0
i, Sum = 0 , 0
for i in range (n):
Sum + = arr[i]
for i in range (n):
if ( abs (arr[i] - ( Sum - arr[i])) > k):
cnt + = 1
return cnt
arr = [ 1 , 3 , 5 ]
n = len (arr)
k = 1
print (countAnomalies(arr, n, k))
|
C#
using System;
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()
{
int [] arr = { 1, 3, 5 };
int n = arr.Length;
int k = 1;
Console.WriteLine(countAnomalies(arr, n, k));
}
}
|
PHP
<?php
function countAnomalies( $arr , $n , $k )
{
$cnt = 0;
$sum = 0;
for ( $i = 0; $i < $n ; $i ++)
$sum += $arr [ $i ];
for ( $i = 0; $i < $n ; $i ++)
if ( abs ( $arr [ $i ] -
( $sum - $arr [ $i ])) > $k )
$cnt ++;
return $cnt ;
}
$arr = array (1, 3, 5);
$n = count ( $arr );
$k = 1;
echo countAnomalies( $arr , $n , $k );
?>
|
Javascript
<script>
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));
</script>
|
Time Complexity : O(n)
Auxiliary Space : O(1)
Share your thoughts in the comments
Please Login to comment...