Count array elements whose product of digits is a Composite Number
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:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#include <set>
using namespace std;
#define N 100005
void SieveOfEratosthenes( bool prime[],
int p_size)
{
prime[0] = false ;
prime[1] = false ;
for ( int p = 2; p * p <= p_size; p++) {
if (prime[p]) {
for ( int i = p * 2; i <= p_size; i += p)
prime[i] = false ;
}
}
}
long long int digitProduct( int number)
{
long long int res = 1;
while (number > 0) {
res *= (number % 10);
number /= 10;
}
return res;
}
void DistinctCompositeDigitProduct( int arr[],
int n)
{
set< int > output;
bool prime[N + 1];
memset (prime, true , sizeof (prime));
SieveOfEratosthenes(prime, N);
for ( int i = 0; i < n; i++) {
long long int ans
= digitProduct(arr[i]);
if (ans <= 1) {
continue ;
}
if (!prime[ans]) {
output.insert(ans);
}
}
cout << output.size() << endl;
}
int main()
{
int arr[]
= { 13, 55, 7, 13, 11,
71, 233, 233, 144, 89 };
int n = sizeof (arr)
/ sizeof (arr[0]);
DistinctCompositeDigitProduct(arr, n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static int N = 100005 ;
static void SieveOfEratosthenes( boolean prime[],
int p_size)
{
prime[ 0 ] = false ;
prime[ 1 ] = false ;
for ( int p = 2 ; p * p <= p_size; p++)
{
if (prime[p])
{
for ( int i = p * 2 ; i <= p_size; i += p)
prime[i] = false ;
}
}
}
static int digitProduct( int number)
{
int res = 1 ;
while (number > 0 )
{
res *= (number % 10 );
number /= 10 ;
}
return res;
}
static void DistinctCompositeDigitProduct( int arr[],
int n)
{
TreeSet<Integer> output = new TreeSet<Integer>();
boolean prime[] = new boolean [N + 1 ];
Arrays.fill(prime, true );
SieveOfEratosthenes(prime, N);
for ( int i = 0 ; i < n; i++)
{
int ans = digitProduct(arr[i]);
if (ans <= 1 )
{
continue ;
}
if (!prime[ans])
{
output.add(ans);
}
}
System.out.print(output.size());
}
public static void main(String[] args)
{
int arr[] = { 13 , 55 , 7 , 13 , 11 ,
71 , 233 , 233 , 144 , 89 };
int n = arr.length;
DistinctCompositeDigitProduct(arr, n);
}
}
|
Python3
N = 100005
from math import sqrt
def SieveOfEratosthenes(prime, p_size):
prime[ 0 ] = False
prime[ 1 ] = False
for p in range ( 2 , int (sqrt(p_size)), 1 ):
if (prime[p]):
for i in range (p * 2 , p_size + 1 , p):
prime[i] = False
def digitProduct(number):
res = 1
while (number > 0 ):
res * = (number % 10 )
number / / = 10
return res
def DistinctCompositeDigitProduct(arr, n):
output = set ()
prime = [ True for i in range (N + 1 )]
SieveOfEratosthenes(prime, N)
for i in range (n):
ans = digitProduct(arr[i])
if (ans < = 1 ):
continue
if (prime[ans] = = False ):
output.add(ans)
print ( len (output))
if __name__ = = '__main__' :
arr = [ 13 , 55 , 7 , 13 , 11 , 71 , 233 , 233 , 144 , 89 ]
n = len (arr)
DistinctCompositeDigitProduct(arr, n)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int N = 100005;
static void SieveOfEratosthenes( bool [] prime,
int p_size)
{
prime[0] = false ;
prime[1] = false ;
for ( int p = 2; p * p <= p_size; p++)
{
if (prime[p])
{
for ( int i = p * 2; i <= p_size; i += p)
prime[i] = false ;
}
}
}
static int digitProduct( int number)
{
int res = 1;
while (number > 0)
{
res *= (number % 10);
number /= 10;
}
return res;
}
static void DistinctCompositeDigitProduct( int [] arr,
int n)
{
SortedSet< int > output = new SortedSet< int >();
bool [] prime = new bool [N + 1];
for ( int i = 0; i < N + 1; i++)
{
prime[i] = true ;
}
SieveOfEratosthenes(prime, N);
for ( int i = 0; i < n; i++)
{
int ans = digitProduct(arr[i]);
if (ans <= 1)
{
continue ;
}
if (!prime[ans])
{
output.Add(ans);
}
}
Console.WriteLine(output.Count);
}
public static void Main()
{
int [] arr = { 13, 55, 7, 13, 11,
71, 233, 233, 144, 89 };
int n = arr.Length;
DistinctCompositeDigitProduct(arr, n);
}
}
|
Javascript
<script>
var N = 100005;
function SieveOfEratosthenes(prime, p_size)
{
prime[0] = false ;
prime[1] = false ;
var p,i;
for (p = 2; p * p <= p_size; p++) {
if (prime[p]) {
for (i = p * 2; i <= p_size; i += p)
prime[i] = false ;
}
}
}
function digitProduct(number)
{
var res = 1;
while (number > 0) {
res *= (number % 10);
number = parseInt(number/10);
}
return res;
}
function DistinctCompositeDigitProduct(arr, n)
{
var output = new Set();
var prime = Array(N + 1).fill( true );
SieveOfEratosthenes(prime, N);
var i;
for (i = 0; i < n; i++) {
var ans = digitProduct(arr[i]);
if (ans <= 1) {
continue ;
}
if (prime[ans]== false ) {
output.add(ans);
}
}
document.write(output.size);
}
var arr = [13, 55, 7, 13, 11,
71, 233, 233, 144, 89];
var n = arr.length;
DistinctCompositeDigitProduct(arr, n);
</script>
|
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)
Last Updated :
27 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...