Given an array A of N integers. An anomaly is a number for which the absolute difference between it and every other number in the array is greater than K where k is a given positive integer. Find the number of anomalies.
Examples:
Input : arr[] = {1, 3, 5}, k = 1
Output : 3
Explanation:
All the numbers in the array are anomalies because
For the number 1 abs(1-3)=2, abs(1-5)=4 which all are greater than 1,
For the number 3 abs(3-1)=2, abs(3-5)=2 which all are again greater than 1
For the number 5 abs(5-1)=4, abs(5-3)=2 which all are again greater than 1
So there are 3 anomalies.
Input : arr[] = {7, 1, 8}, k = 5
Output : 1
Simple Approach: We simply check for each number if it satisfies the given condition, that is, the absolute difference is greater than K or not with each of the other numbers.
C++
#include<iostream>
using namespace std;
int countAnomalies( int arr[], int n, int k)
{
int res = 0;
for ( int i=0; i<n; i++)
{
int j;
for (j=0; j<n; j++)
if (i != j && abs (arr[i] - arr[j]) <= k)
break ;
if (j == n)
res++;
}
return res;
}
int main()
{
int arr[] = {7, 1, 8}, k = 5;
int n = sizeof (arr)/ sizeof (arr[0]);
cout << countAnomalies(arr, n, k);
return 0;
}
|
Java
class GFG
{
static int countAnomalies( int arr[],
int n, int k)
{
int res = 0 ;
for ( int i = 0 ; i < n; i++)
{
int j;
for (j = 0 ; j < n; j++)
if (i != j && Math.abs(arr[i] -
arr[j]) <= k)
break ;
if (j == n)
res++;
}
return res;
}
public static void main(String args[])
{
int arr[] = { 7 , 1 , 8 }, k = 5 ;
int n = arr.length;
System.out.println(countAnomalies(arr, n, k));
}
}
|
Python3
def countAnomalies(arr, n, k):
res = 0
for i in range ( 0 , n):
j = 0
while j < n:
if i ! = j and abs (arr[i] - arr[j]) < = k:
break
j + = 1
if j = = n:
res + = 1
return res
if __name__ = = "__main__" :
arr = [ 7 , 1 , 8 ]
k = 5
n = len (arr)
print (countAnomalies(arr, n, k))
|
C#
using System;
class GFG
{
static int countAnomalies( int [] arr,
int n, int k)
{
int res = 0;
for ( int i = 0; i < n; i++)
{
int j;
for (j = 0; j < n; j++)
if (i != j && Math.Abs(arr[i] -
arr[j]) <= k)
break ;
if (j == n)
res++;
}
return res;
}
public static void Main()
{
int [] arr = {7, 1, 8};
int k = 5;
int n = arr.Length;
Console.WriteLine(countAnomalies(arr, n, k));
}
}
|
PHP
<?php
function countAnomalies(& $arr , $n , $k )
{
$res = 0;
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = 0; $j < $n ; $j ++)
if ( $i != $j && abs ( $arr [ $i ] -
$arr [ $j ]) <= $k )
break ;
if ( $j == $n )
$res ++;
}
return $res ;
}
$arr = array (7, 1, 8);
$k = 5;
$n = sizeof( $arr );
echo countAnomalies( $arr , $n , $k );
?>
|
Javascript
<script>
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));
</script>
|
Time Complexity: O(n * n)
Auxiliary Space: O(1)
Efficient Approach using Binary Search
- Sort the array.
- For every element, find the largest element greater than it and the smallest element greater than it. We can find these two in O(Log n) time using Binary Search. If the difference between these two elements is more than k, we increment the result.
Prerequisites for below C++ code : lower_bound in C++, upper_bound in C++.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
int countAnomalies( int a[], int n, int k)
{
sort(a, a+n);
int res = 0;
for ( int i=0; i<n; i++)
{
int *u = upper_bound(a, a+n, a[i]);
if (u != a+n && ((*u) - a[i]) <= k)
continue ;
int *s = lower_bound(a, a+n, a[i]);
if (u - s > 1)
continue ;
if (s != a && (*(s - 1) - a[i]) <= k)
continue ;
res++;
}
return res;
}
int main()
{
int arr[] = {7, 1, 8}, k = 5;
int n = sizeof (arr)/ sizeof (arr[0]);
cout << countAnomalies(arr, n, k);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int countAnomalies( int a[], int n, int k)
{
Arrays.sort(a);
int res = 0 ;
for ( int i = 0 ; i < n; i++)
{
int u = upper_bound(a, 0 , n, a[i]);
if (u < n && a[u] - a[i] <= k)
continue ;
int s = lower_bound(a, 0 , n, a[i]);
if (u - s > 1 )
continue ;
if (s > 0 && a[s - 1 ] - a[i] <= k)
continue ;
res++;
}
return res;
}
static int lower_bound( int [] a, int low,
int high, int element)
{
while (low < high)
{
int middle = low + (high - low) / 2 ;
if (element > a[middle])
low = middle + 1 ;
else
high = middle;
}
return low;
}
static int upper_bound( int [] a, int low,
int high, int element)
{
while (low < high)
{
int middle = low + (high - low) / 2 ;
if (a[middle] > element)
high = middle;
else
low = middle + 1 ;
}
return low;
}
public static void main(String[] args)
{
int arr[] = { 7 , 1 , 8 }, k = 5 ;
int n = arr.length;
System.out.print(countAnomalies(arr, n, k));
}
}
|
Python3
def countAnomalies(a, n, k):
a = sorted (a);
res = 0 ;
for i in range (n):
u = upper_bound(a, 0 ,
n, a[i]);
if (u < n and
a[u] - a[i] < = k):
continue ;
s = lower_bound(a, 0 ,
n, a[i]);
if (u - s > 1 ):
continue ;
if (s > 0 and
a[s - 1 ] - a[i] < = k):
continue ;
res + = 1 ;
return res;
def lower_bound(a, low,
high, element):
while (low < high):
middle = int (low +
int (high - low) / 2 );
if (element > a[middle]):
low = middle + 1 ;
else :
high = middle;
return low;
def upper_bound(a, low,
high, element):
while (low < high):
middle = int (low +
(high - low) / 2 );
if (a[middle] > element):
high = middle;
else :
low = middle + 1 ;
return low;
if __name__ = = '__main__' :
arr = [ 7 , 1 , 8 ]
k = 5 ;
n = len (arr);
print (countAnomalies(arr,
n, k));
|
C#
using System;
class GFG{
static int countAnomalies( int []a, int n, int k)
{
Array.Sort(a);
int res = 0;
for ( int i = 0; i < n; i++)
{
int u = upper_bound(a, 0, n, a[i]);
if (u < n && a[u] - a[i] <= k)
continue ;
int s = lower_bound(a, 0, n, a[i]);
if (u - s > 1)
continue ;
if (s > 0 && a[s - 1] - a[i] <= k)
continue ;
res++;
}
return res;
}
static int lower_bound( int [] a, int low,
int high, int element)
{
while (low < high)
{
int middle = low + (high - low) / 2;
if (element > a[middle])
low = middle + 1;
else
high = middle;
}
return low;
}
static int upper_bound( int [] a, int low,
int high, int element)
{
while (low < high)
{
int middle = low + (high - low) / 2;
if (a[middle] > element)
high = middle;
else
low = middle + 1;
}
return low;
}
public static void Main(String[] args)
{
int []arr = { 7, 1, 8 };
int k = 5;
int n = arr.Length;
Console.Write(countAnomalies(arr, n, k));
}
}
|
Javascript
<script>
function countAnomalies(a, n, k)
{
a.sort();
let res = 0;
for (let i = 0; i < n; i++)
{
let u = upper_bound(a, 0, n, a[i]);
if (u < n && a[u] - a[i] <= k)
continue ;
let s = lower_bound(a, 0, n, a[i]);
if (u - s > 1)
continue ;
if (s > 0 && a[s - 1] - a[i] <= k)
continue ;
res++;
}
return res;
}
function lower_bound(a, low,
high, element)
{
while (low < high)
{
let middle = low + Math.floor((high - low) / 2);
if (element > a[middle])
low = middle + 1;
else
high = middle;
}
return low;
}
function upper_bound(a, low, high, element)
{
while (low < high)
{
let middle = low + Math.floor((high - low) / 2);
if (a[middle] > element)
high = middle;
else
low = middle + 1;
}
return low;
}
let arr = [ 7, 1, 8 ], k = 5;
let n = arr.length;
document.write(countAnomalies(arr, n, k));
</script>
|
Time Complexity: O(n Log n)
Auxiliary Space: O(1)
Another efficient solution for small k
- Insert all values of the array in a hash table.
- Traverse array again and for every value arr[i], search for every value from arr[i] – k to arr[i] + k (excluding arr[i]). If none of the elements are found, then arr[i] is anomaly.
Time Complexity: O(n k)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!