# Count of Array elements divisible by either their product or sum of digits

• Difficulty Level : Basic
• Last Updated : 31 Jan, 2022

Given an array arr[]. The task is to count the elements in the array that are divisible by either their product of digits or the sum of digits.

Example:

Input: arr[] = {123, 25, 36, 7}
Output: 2
Explanation: Following are the elements that follows the given conditions
Sum of digits of 123 is 6 and the product of digits is also 6.
Since 123 is not divisible by 6, so it is not counted.
The sum of digits of 36 is 9 and product of digits is 18. Since 36 is divisible by both, so it is considered in answer.
Similarly, sum of digits and product of digits of 7 is 7 itself, hence it is also considered.
Hence, 2 is the final answer.

Input: arr[] = {10, 22, 15}
Output: 2
Explanation:  The sum of digits in 10 is 1, and 10 is divisible by 1, hence we consider it. The product of digits of 15 is 5, and 15 is divisible by 5, we consider it in our output.

Approach: This problem is observation-based. Follow the steps below to solve the given problem.

• Declare a variable say count = 0, to store the number of elements satisfying given conditions.
• For every number in the array arr[], find the sum of its digits and the product of its digits.
• Check whether the number is divisible by either its sum of digits or the product of digits.
• Return the count as the final answer.

Below is the implementation of the above approach.

## C++

 `// C++ program for above approach``#include ``using` `namespace` `std;` `// Function to find the number of elements``// in arr[] that follows the given conditions``int` `solve(``int` `arr[], ``int` `n)``{` `    ``// To count the number of elements``    ``// that follow the given conditions``    ``int` `count = 0;` `    ``// Iterate through the array arr[]``    ``for` `(``int` `i = 0; i < n; i++) {``        ``int` `p = arr[i];``        ``int` `sum = 0, prod = 1;``        ``while` `(p > 0) {``            ``int` `r = p % 10;``            ``sum += r;``            ``prod *= r;``            ``p /= 10;``        ``}` `        ``// Check if condition satisfies or not``        ``if` `((sum > 0 && arr[i] % sum == 0)``            ``|| (prod > 0 && arr[i] % prod == 0))``            ``count++;``    ``}` `    ``// Return count as the final answer``    ``return` `count;``}` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 123, 25, 36, 7 };``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr[0]);` `    ``// Function Call``    ``cout << solve(arr, N) << endl;` `    ``return` `0;``}`

## Java

 `// Java program for above approach``import` `java.util.*;``public` `class` `GFG {` `  ``// Function to find the number of elements``  ``// in arr[] that follows the given conditions``  ``static` `int` `solve(``int``[] arr, ``int` `n)``  ``{` `    ``// To count the number of elements``    ``// that follow the given conditions``    ``int` `count = ``0``;` `    ``// Iterate through the array arr[]``    ``for` `(``int` `i = ``0``; i < n; i++) {``      ``int` `p = arr[i];``      ``int` `sum = ``0``, prod = ``1``;``      ``while` `(p > ``0``) {``        ``int` `r = p % ``10``;``        ``sum += r;``        ``prod *= r;``        ``p /= ``10``;``      ``}` `      ``// Check if condition satisfies or not``      ``if` `((sum > ``0` `&& arr[i] % sum == ``0``)``          ``|| (prod > ``0` `&& arr[i] % prod == ``0``))``        ``count++;``    ``}` `    ``// Return count as the final answer``    ``return` `count;``  ``}` `  ``// Driver Code``  ``public` `static` `void` `main(String args[])``  ``{``    ``int``[] arr = { ``123``, ``25``, ``36``, ``7` `};``    ``int` `N = arr.length;` `    ``// Function Call``    ``System.out.println(solve(arr, N));``  ``}``}` `// This code is contributed by Samim Hossain Mondal.`

## Python3

 `# python3 program for above approach` `# Function to find the number of elements``# in arr[] that follows the given conditions``def` `solve(arr, n):` `        ``# To count the number of elements``        ``# that follow the given conditions``    ``count ``=` `0` `    ``# Iterate through the array arr[]``    ``for` `i ``in` `range``(``0``, n):``        ``p ``=` `arr[i]``        ``sum` `=` `0``        ``prod ``=` `1``        ``while` `(p > ``0``):``            ``r ``=` `p ``%` `10``            ``sum` `+``=` `r``            ``prod ``*``=` `r``            ``p ``/``/``=` `10` `            ``# Check if condition satisfies or not``        ``if` `((``sum` `> ``0` `and` `arr[i] ``%` `sum` `=``=` `0``)``                ``or` `(prod > ``0` `and` `arr[i] ``%` `prod ``=``=` `0``)):``            ``count ``+``=` `1` `        ``# Return count as the final answer``    ``return` `count` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:` `    ``arr ``=` `[``123``, ``25``, ``36``, ``7``]``    ``N ``=` `len``(arr)` `    ``# Function Call``    ``print``(solve(arr, N))` `    ``# This code is contributed by rakeshsahni`

## C#

 `// C# program for above approach``using` `System;``class` `GFG {` `  ``// Function to find the number of elements``  ``// in arr[] that follows the given conditions``  ``static` `int` `solve(``int``[] arr, ``int` `n)``  ``{` `    ``// To count the number of elements``    ``// that follow the given conditions``    ``int` `count = 0;` `    ``// Iterate through the array arr[]``    ``for` `(``int` `i = 0; i < n; i++) {``      ``int` `p = arr[i];``      ``int` `sum = 0, prod = 1;``      ``while` `(p > 0) {``        ``int` `r = p % 10;``        ``sum += r;``        ``prod *= r;``        ``p /= 10;``      ``}` `      ``// Check if condition satisfies or not``      ``if` `((sum > 0 && arr[i] % sum == 0)``          ``|| (prod > 0 && arr[i] % prod == 0))``        ``count++;``    ``}` `    ``// Return count as the final answer``    ``return` `count;``  ``}` `  ``// Driver Code``  ``public` `static` `void` `Main()``  ``{``    ``int``[] arr = { 123, 25, 36, 7 };``    ``int` `N = arr.Length;` `    ``// Function Call``    ``Console.Write(solve(arr, N));``  ``}``}` `// This code is contributed by ukasp.`

## Javascript

 ``

Output
`2`

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up