# Count the factors of K present in the given Array

Given an array arr[] and an integer K, the task is to calculate the count the factors of K present in the array.

Examples:

Input: arr[] = {1, 2, 4, 5, 6}, K = 6
Output: 3
Explanation:
There are three numbers present in the array those are factors of K = 6 – {1, 2, 6}

Input: arr[] = {1, 2, 12, 24}, K = 20
Output: 2
Explanation:
There are two numbers present in the array those are factors of K = 20 – {1, 2}

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive Approach: A simple solution for this problem is to find all the factors of K and then for each factor iterate over the array and check that it is present in the array or not. If yes then increment the count of factors by 1.

Efficient Approach: The idea is to instead of finding all factors of the number K iterate over the array and check for each element that it is the factor of K, or not with the help of the modulo operator. If yes then increment the count of factors of K.

Below is the implementation of the above approach:

## C++

 `// C++ implementaion to find the count ` `// of factors of K present in array ` ` `  `#include ` `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 implementaion 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 implementaion 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# implementaion 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 `

Output:

```3
```

Time Complexity: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.