Dada una array arr[] de N enteros y un entero K , la tarea es encontrar el número de anomalías en la array. Una anomalía es un número para el cual la diferencia absoluta entre él y todos los demás números de la array es mayor que K . Encuentre el número de anomalías.
Ejemplos:
Entrada: arr[] = {1, 3, 5}, k = 1
Salida: 2
1 y 3 son las anomalías.
|1 – (3 + 5)| = 7 > 1
|3 – (1 + 5)| = 3 > 1Entrada: arr[] = {7, 1, 8}, k = 5
Salida: 1
Enfoque: Encuentre la suma de todos los elementos del arreglo y guárdelo en sum , ahora para cada elemento del arreglo arr[i] si la diferencia absoluta de arr[i] con sum – arr[i] es > k entonces es un anomalía. Cuente todas las anomalías en la array e imprima el resultado al final.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include<bits/stdc++.h> 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
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 = 1 print(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 ?>
Javascript
<script> // Javascript implementation of the approach // Function to return the number of anomalies function countAnomalies(arr, n, k) { // To store the count of anomalies var cnt = 0; // To store the sum of the array elements var 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 var arr = [ 1, 3, 5 ]; var n = arr.length; var k = 1; document.write(countAnomalies(arr, n, k)); // This code is contributed by umadevi9616 </script>
2
Tiempo Complejidad : O(n)
Espacio Auxiliar : O(1)
Publicación traducida automáticamente
Artículo escrito por facebookruppal y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA