Given an array arr[] consisting of N non-negative integers, the task is to count the number of array elements whose product of digits is a composite number.
Examples:
Input: arr[] = {13, 55, 7, 13, 11, 71, 233, 144, 89}
Output: 4
Explanation: The array elements having product of digits equal to a composite number are 55 (5 * 5 = 25), 233 (2 * 3 * 3 = 18), 144 (1 * 4 * 4 = 16), 89 ( = 8 * 9 = 72)Input: arr[] = {34, 13, 55, 11, 8, 3, 55, 23}
Output: 3
Explanation: The array elements having product of digits equal to a composite number are 34 ( = 3 * 4 = 12), 55 ( 5 * 5 = 25), 23 ( = 2 * 3 = 6)
Approach: Follow the steps below to solve the problem:
- Precompute and store all the prime numbers using sieve of Eratosthenes.
- Create a Set to store all the distinct array elements whose product of digits is a composite number.
- Traverse the given array and for each array element, check if the product of all its digits is prime number or not. If found to be true, add the current element to the Set.
- Otherwise, proceed to the next array element.
- After traversing, print the size of the set as the required answer.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> #include <set> using namespace std; #define N 100005 // Function to generate prime numbers // using Sieve of Eratosthenes void SieveOfEratosthenes(bool prime[], int p_size) { // Set 0 and 1 as non-prime prime[0] = false; prime[1] = false; for (int p = 2; p * p <= p_size; p++) { // If p is a prime if (prime[p]) { // Set all multiples of p as non-prime for (int i = p * 2; i <= p_size; i += p) prime[i] = false; } } } // Function to calculate the product // of digits of the given number long long int digitProduct(int number) { // Stores the product of digits long long int res = 1; while (number > 0) { // Extract digits and // add to the sum res *= (number % 10); number /= 10; } // Return the product // of its digits return res; } // Function to print number of distinct // values with digit product as composite void DistinctCompositeDigitProduct(int arr[], int n) { // Initialize set set<int> output; // Initialize boolean array bool prime[N + 1]; memset(prime, true, sizeof(prime)); // Pre-compute primes SieveOfEratosthenes(prime, N); // Traverse array for (int i = 0; i < n; i++) { // Stores the product of digits // of the current array element long long int ans = digitProduct(arr[i]); // If Product of digits is // less than or equal to 1 if (ans <= 1) { continue; } // If Product of digits is // not a prime if (!prime[ans]) { output.insert(ans); } } // Print the answer cout << output.size() << endl; } // Driver Code int main() { // Given array int arr[] = { 13, 55, 7, 13, 11, 71, 233, 233, 144, 89 }; // Given size int n = sizeof(arr) / sizeof(arr[0]); // Function call DistinctCompositeDigitProduct(arr, n); return 0; } |
4
Time Complexity: O(N + MlogM), where N is the size of the given array and M is the maximum array element.
Auxiliary Space: O(1)
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.
Recommended Posts:
- Count pairs in an array whose product is composite number
- Count of all subsequence whose product is a Composite number
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Find a sequence of N prime numbers whose sum is a composite number
- Length of longest subsequence having sum of digits of each element as a Composite Number
- Sum and Product of all Composite numbers which are divisible by k in an array
- Product of all the Composite Numbers in an array
- Sum and product of k smallest and k largest composite numbers in the array
- Count and Sum of composite elements in an array
- Maximize product of array by replacing array elements with its sum or product with element from another array
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Count arrays of length K whose product of elements is same as that of given array
- Count unique paths is a matrix whose product of elements contains odd number of divisors
- Sum of elements in an array having composite frequency
- Minimum digits to be removed to make either all digits or alternating digits same
- Count composite fibonacci numbers from given array
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Find the maximum number of composite summands of a number
- Find the total number of composite factor for a given number
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.

