

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Number of anomalies in an array in C++
In this tutorial, we are going to write a program that finds the number of anomalies in the given array.
A number is an anomaly in the given array if the absolute difference between the number and all other numbers is greater than the given number k. Let's see an example.
Input
arr = [3, 1, 5, 7] k = 1
Output
4
The absolute difference between all numbers with others is greater than k.
Algorithm
Initialise the array.
Iterate over the array.
Take the element and iterate over the array.
Find the absolute difference between the two numbers.
If no absolute difference is less than or equal to k than increment the count of anomalies.
Implementation
Following is the implementation of the above algorithm in C++
#include <bits/stdc++.h> using namespace std; int getAnomaliesCount(int arr[], int n, int k) { int count = 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) { count++; } } return count; } int main() { int arr[] = {3, 1, 5, 7}, k = 1; int n = 4; cout << getAnomaliesCount(arr, n, k) << endl; return 0; }
Output
If you run the above code, then you will get the following result.
4
- Related Questions & Answers
- What are the causes of Anomalies?
- Count number of primes in an array in C++
- Total number of elements present in an array in C#
- C# program to count number of bytes in an array
- Kth odd number in an array in C++
- Total number of elements in a specified dimension of an Array in C#
- Find the frequency of a number in an array using C++.
- Minimum number of swaps required to sort an array in C++
- Find the Number of Prime Pairs in an Array using C++
- Find the Number of Unique Pairs in an Array using C++
- Count number of even and odd elements in an array in C++
- Number of vowels within an array in JavaScript
- How do you find the number of dimensions of an array in C#?
- Maximum and minimum of an array using minimum number of comparisons in C
- An array of streams in C#
Advertisements