Dada una array arr[] y un entero K , la tarea es calcular el recuento de los factores de K presentes en la array.
Ejemplos:
Entrada: arr[] = {1, 2, 4, 5, 6}, K = 6
Salida: 3
Explicación:
Hay tres números presentes en la array que son factores de K = 6 – {1, 2, 6}
Entrada : arr[] = {1, 2, 12, 24}, K = 20
Salida: 2
Explicación:
Hay dos números presentes en la array que son factores de K = 20 – {1, 2}
Enfoque ingenuo: una solución simple para este problema es encontrar todos los factores de K y luego, para cada factor, iterar sobre la array y verificar si está presente en la array o no. En caso afirmativo, incremente el recuento de factores en 1.
Enfoque eficiente: la idea es, en lugar de encontrar todos los factores del número K , iterar sobre la array y verificar para cada elemento si es el factor de K, o no con la ayuda de el operador módulo . En caso afirmativo, incremente el recuento de factores de K.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation to find the count // of factors of K present in array #include <iostream> using namespace std; // Function to find the count // of factors of K present in array int calcCount(int arr[], int n, int k) { int count = 0; // Loop to consider every // element of array for (int i = 0; i < n; i++) { if (k % arr[i] == 0) count++; } return count; } // Driver Code int main() { int arr[] = { 1, 2, 4, 5, 6 }; int n = sizeof(arr) / sizeof(arr[0]); int k = 6; // Function Call cout << calcCount(arr, n, k); return 0; }
Java
// Java implementation to find the count // of factors of K present in array class GFG{ // Function to find the count // of factors of K present in array static int calcCount(int arr[], int n, int k) { int count = 0; // Loop to consider every // element of array for(int i = 0; i < n; i++) { if (k % arr[i] == 0) count++; } return count; } // Driver Code public static void main(String[] args) { int arr[] = { 1, 2, 4, 5, 6 }; int n = arr.length; int k = 6; // Function Call System.out.print(calcCount(arr, n, k)); } } // This code is contributed by gauravrajput1
Python3
# Python3 implementation to find the count # of factors of K present in array # Function to find the count # of factors of K present in array def calcCount(arr, n, k): count = 0 # Loop to consider every # element of array for i in range(0, n): if (k % arr[i] == 0): count = count + 1 return count # Driver Code arr = [ 1, 2, 4, 5, 6 ] n = len(arr) k = 6 # Function Call print(calcCount(arr, n, k)) # This code is contributed by PratikBasu
C#
// C# implementation to find the count // of factors of K present in array using System; class GFG{ // Function to find the count // of factors of K present in array static int calcCount(int []arr, int n, int k) { int count = 0; // Loop to consider every // element of array for(int i = 0; i < n; i++) { if (k % arr[i] == 0) count++; } return count; } // Driver Code public static void Main(String[] args) { int []arr = { 1, 2, 4, 5, 6 }; int n = arr.Length; int k = 6; // Function Call Console.Write(calcCount(arr, n, k)); } } // This code is contributed by Amit Katiyar
Javascript
<script> // Javascript implementation to find the count // of factors of K present in array // Function to find the count // of factors of K present in array function calcCount(arr, n, k) { var count = 0; // Loop to consider every // element of array for (var i = 0; i < n; i++) { if (k % arr[i] == 0) count++; } return count; } // Driver Code var arr = [ 1, 2, 4, 5, 6 ]; var n = arr.length; var k = 6; // Function Call document.write( calcCount(arr, n, k)); </script>
3
Complejidad de tiempo: O(N)
Espacio Auxiliar: O(1)