给定一个由N个整数组成的数组A。异常是一个数字,其与数组中每个其他数字之间的绝对差大于K,其中k是给定的正整数。查找异常数量。
例子:
Input : arr[] = {1, 3, 5}, k = 1
Output : 3
Explanation:
All the numbers in the array are anamolies 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 anamolies.
Input : arr[] = {7, 1, 8}, k = 5
Output : 1
简单方法:
我们简单地检查每个数字是否满足给定条件,即每个其他数字的绝对差大于K或不大于K。
C++
// A simple C++ solution to count anomalies in
// an array.
#include
using namespace std;
int countAnomalies(int arr[], int n, int k)
{
int res = 0;
for (int i=0; i
Java
// A simple java solution to count
// anomalies in an array.
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;
}
// Driver code
public static void main(String args[])
{
int arr[] = {7, 1, 8}, k = 5;
int n = arr.length;
System.out.println(countAnomalies(arr, n, k));
}
}
// This code is contributed by ANKITRAI1
Python3
# A simple Python3 solution to
# count anomalies in an array.
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
# Driver Code
if __name__ == "__main__":
arr = [7, 1, 8]
k = 5
n = len(arr)
print(countAnomalies(arr, n, k))
# This code is contributed by Rituraj Jain
C#
// A simple C# solution to count
// anomalies in an array.
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;
}
// Driver code
public static void Main()
{
int[] arr = {7, 1, 8};
int k = 5;
int n = arr.Length;
Console.WriteLine(countAnomalies(arr, n, k));
}
}
// This code is contributed
// by Akanksha Rai(Abby_akku)
PHP
C++
#include
using namespace std;
int countAnomalies(int a[], int n, int k)
{
// Sort the array so that we can apply binary
// search.
sort(a, a+n);
// One by one check every element if it is
// anomaly or not using binary search.
int res = 0;
for (int i=0; i 1)
continue;
// If arr[i] is not smallest element and
// just smaller element is not k distance away
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)
{
// Sort the array so that we can apply binary
// search.
Arrays.sort(a);
// One by one check every element if it is
// anomaly or not using binary search.
int res = 0;
for (int i = 0; i < n; i++)
{
int u = upper_bound(a, 0, n, a[i]);
// If arr[i] is not largest element and
// element just greater than it is within
// k, then return false.
if (u < n && a[u] - a[i] <= k)
continue;
int s = lower_bound(a, 0, n, a[i]);
// If there are more than one occurrences
// of arr[i], return false.
if (u - s > 1)
continue;
// If arr[i] is not smallest element and
// just smaller element is not k distance away
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));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to implement
# the above approach
def countAnomalies(a, n, k):
# Sort the array so that
# we can apply binary
# search.
a = sorted(a);
# One by one check every
# element if it is anomaly
# or not using binary search.
res = 0;
for i in range(n):
u = upper_bound(a, 0,
n, a[i]);
# If arr[i] is not largest
# element and element just
# greater than it is within
# k, then return False.
if (u < n and
a[u] - a[i] <= k):
continue;
s = lower_bound(a, 0,
n, a[i]);
# If there are more than
# one occurrences of arr[i],
# return False.
if (u - s > 1):
continue;
# If arr[i] is not smallest
# element and just smaller
# element is not k distance away
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;
# Driver code
if __name__ == '__main__':
arr = [7, 1, 8]
k = 5;
n = len(arr);
print(countAnomalies(arr,
n, k));
# This code is contributed by shikhasingrajput
C#
using System;
class GFG{
static int countAnomalies(int []a, int n, int k)
{
// Sort the array so that we can
// apply binary search.
Array.Sort(a);
// One by one check every element if it is
// anomaly or not using binary search.
int res = 0;
for(int i = 0; i < n; i++)
{
int u = upper_bound(a, 0, n, a[i]);
// If arr[i] is not largest element and
// element just greater than it is within
// k, then return false.
if (u < n && a[u] - a[i] <= k)
continue;
int s = lower_bound(a, 0, n, a[i]);
// If there are more than one occurrences
// of arr[i], return false.
if (u - s > 1)
continue;
// If arr[i] is not smallest element and
// just smaller element is not k distance away
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;
}
// Driver code
public static void Main(String[] args)
{
int []arr = { 7, 1, 8 };
int k = 5;
int n = arr.Length;
Console.Write(countAnomalies(arr, n, k));
}
}
// This code is contributed by Amit Katiyar
输出:
1
时间复杂度: O(n * n)
使用二进制搜索的有效方法
1)对数组进行排序。
2)对于每个元素,找到大于它的最大元素,并找到大于它的最小元素。我们可以使用Binary Search在O(Log n)时间中找到这两个。如果这两个元素之间的差大于k,我们将增加结果。
以下C++代码的先决条件:C++中的lower_bound,C++中的upper_bound。
C++
#include
using namespace std;
int countAnomalies(int a[], int n, int k)
{
// Sort the array so that we can apply binary
// search.
sort(a, a+n);
// One by one check every element if it is
// anomaly or not using binary search.
int res = 0;
for (int i=0; i 1)
continue;
// If arr[i] is not smallest element and
// just smaller element is not k distance away
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)
{
// Sort the array so that we can apply binary
// search.
Arrays.sort(a);
// One by one check every element if it is
// anomaly or not using binary search.
int res = 0;
for (int i = 0; i < n; i++)
{
int u = upper_bound(a, 0, n, a[i]);
// If arr[i] is not largest element and
// element just greater than it is within
// k, then return false.
if (u < n && a[u] - a[i] <= k)
continue;
int s = lower_bound(a, 0, n, a[i]);
// If there are more than one occurrences
// of arr[i], return false.
if (u - s > 1)
continue;
// If arr[i] is not smallest element and
// just smaller element is not k distance away
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));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to implement
# the above approach
def countAnomalies(a, n, k):
# Sort the array so that
# we can apply binary
# search.
a = sorted(a);
# One by one check every
# element if it is anomaly
# or not using binary search.
res = 0;
for i in range(n):
u = upper_bound(a, 0,
n, a[i]);
# If arr[i] is not largest
# element and element just
# greater than it is within
# k, then return False.
if (u < n and
a[u] - a[i] <= k):
continue;
s = lower_bound(a, 0,
n, a[i]);
# If there are more than
# one occurrences of arr[i],
# return False.
if (u - s > 1):
continue;
# If arr[i] is not smallest
# element and just smaller
# element is not k distance away
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;
# Driver code
if __name__ == '__main__':
arr = [7, 1, 8]
k = 5;
n = len(arr);
print(countAnomalies(arr,
n, k));
# This code is contributed by shikhasingrajput
C#
using System;
class GFG{
static int countAnomalies(int []a, int n, int k)
{
// Sort the array so that we can
// apply binary search.
Array.Sort(a);
// One by one check every element if it is
// anomaly or not using binary search.
int res = 0;
for(int i = 0; i < n; i++)
{
int u = upper_bound(a, 0, n, a[i]);
// If arr[i] is not largest element and
// element just greater than it is within
// k, then return false.
if (u < n && a[u] - a[i] <= k)
continue;
int s = lower_bound(a, 0, n, a[i]);
// If there are more than one occurrences
// of arr[i], return false.
if (u - s > 1)
continue;
// If arr[i] is not smallest element and
// just smaller element is not k distance away
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;
}
// Driver code
public static void Main(String[] args)
{
int []arr = { 7, 1, 8 };
int k = 5;
int n = arr.Length;
Console.Write(countAnomalies(arr, n, k));
}
}
// This code is contributed by Amit Katiyar
输出:
1
时间复杂度: O(n Log n)
小k的另一种有效解决方案
1)将数组的所有值插入哈希表。
2)再次遍历数组,并为每个值arr [i]搜索从arr [i] – k到arr [i] + k(不包括arr [i])的每个值。如果找不到任何元素,则arr [i]是异常的。
时间复杂度:O(nk)