Check if sum of count of digits of array elements is Prime or not
Last Updated :
17 May, 2021
Given an array A[] consisting of N integers, the task is to check if the sum of numbers of digits in each array element is a prime number or not.
Examples:
Input: A[] = {1, 11, 12}
Output: Yes
Explanation: Count of digits of A[0], A[1] and A[2] are 1, 2, 2 respectively. Therefore, total sum of count of digits = 1 + 2 + 2 = 5, which is prime.
Input: A[] = {1, 11, 123}
Output: No
Approach: Follow the steps below to solve the problem:
- Initialize a variable sum, to store the sum of the number of digits of array elements.
- Traverse the array and convert each array element to its equivalent string
- Add the length of every string to sum.
- Check if the value of sum after complete traversal of the array, is prime or not.
- Print Yes if found to be true. Otherwise, print No.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPrime( int n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for ( int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false ;
return true ;
}
void CheckSumPrime( int A[], int N)
{
int sum = 0;
for ( int i = 0; i < N; i++) {
string s = to_string(A[i]);
sum += s.length();
}
if (isPrime(sum)) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
}
int main()
{
int A[] = { 1, 11, 12 };
int N = sizeof (A) / sizeof (A[0]);
CheckSumPrime(A, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static boolean isPrime( int n)
{
if (n <= 1 )
return false ;
if (n <= 3 )
return true ;
if (n % 2 == 0 || n % 3 == 0 )
return false ;
for ( int i = 5 ; i * i <= n; i = i + 6 )
if (n % i == 0 || n % (i + 2 ) == 0 )
return false ;
return true ;
}
static void CheckSumPrime( int [] A, int N)
{
int sum = 0 ;
for ( int i = 0 ; i < N; i++)
{
String s = Integer.toString(A[i]);
sum += s.length();
}
if (isPrime(sum) == true )
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
public static void main(String[] args)
{
int [] A = { 1 , 11 , 12 };
int N = A.length;
CheckSumPrime(A, N);
}
}
|
Python3
import math
def isPrime(n):
if (n < = 1 ):
return False
if (n < = 3 ):
return True
if (n % 2 = = 0 or n % 3 = = 0 ):
return False
for i in range ( 5 , int (math.sqrt(n) + 1 ), 6 ):
if (n % i = = 0 or n % (i + 2 ) = = 0 ):
return False
return True
def CheckSumPrime(A, N):
sum = 0
for i in range ( 0 , N):
s = str (A[i])
sum + = len (s)
if (isPrime( sum ) = = True ):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = '__main__' :
A = [ 1 , 11 , 12 ]
N = len (A)
CheckSumPrime(A, N)
|
C#
using System;
class GFG{
static bool isPrime( int n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for ( int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false ;
return true ;
}
static void CheckSumPrime( int [] A, int N)
{
int sum = 0;
for ( int i = 0; i < N; i++)
{
String s = A[i].ToString();
sum += s.Length;
}
if (isPrime(sum) == true )
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
public static void Main()
{
int [] A = { 1, 11, 12 };
int N = A.Length;
CheckSumPrime(A, N);
}
}
|
Javascript
<script>
function isPrime(n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for (let i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false ;
return true ;
}
function CheckSumPrime(A, N)
{
let sum = 0;
for (let i = 0; i < N; i++) {
let s = new String(A[i]);
sum += s.length;
}
if (isPrime(sum)) {
document.write( "Yes" + "<br>" );
}
else {
document.write( "No" + "<br>" );
}
}
let A = [ 1, 11, 12 ];
let N = A.length
CheckSumPrime(A, N);
</script>
|
Time Complexity: O(N3/2)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...