Check if sum of count of digits of array elements is Prime or not - GeeksforGeeks

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: YesExplanation: 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 stringAdd 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.
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++
Java
Python3
C#
Javascript
// C++ program for the above approach#include <bits/stdc++.h>using namespace std; // Function to check whether// a number is prime or notbool isPrime(int n){    // Corner cases    if (n <= 1)        return false;    if (n <= 3)        return true;     // If given number is a    // multiple of 2 or 3    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;} // Function to check if sum// of count of digits of all// array elements is prime or notvoid CheckSumPrime(int A[], int N){    // Initialize sum with 0    int sum = 0;     // Traverse over the array    for (int i = 0; i < N; i++) {         // Convert array element to string        string s = to_string(A[i]);         // Add the count of        // digits to sum        sum += s.length();    }     // Print the result    if (isPrime(sum)) {         cout << "Yes" << endl;    }    else {         cout << "No" << endl;    }} // Drive Codeint main(){     int A[] = { 1, 11, 12 };     int N = sizeof(A) / sizeof(A[0]);     // Function call    CheckSumPrime(A, N);     return 0;}
// Java program for the above approachimport java.io.*;import java.util.*; class GFG{ // Function to check whether// a number is prime or notstatic boolean isPrime(int n){         // Corner cases    if (n <= 1)        return false;    if (n <= 3)        return true;     // If given number is a    // multiple of 2 or 3    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;} // Function to check if sum// of count of digits of all// array elements is prime or notstatic void CheckSumPrime(int[] A, int N){         // Initialize sum with 0    int sum = 0;     // Traverse over the array    for(int i = 0; i < N; i++)    {                 // Convert array element to string        String s = Integer.toString(A[i]);         // Add the count of        // digits to sum        sum += s.length();    }     // Print the result    if (isPrime(sum) == true)    {        System.out.println("Yes");    }    else    {        System.out.println("No");    }} // Drive Codepublic static void main(String[] args){    int[] A = { 1, 11, 12 };     int N = A.length;     // Function call    CheckSumPrime(A, N);}} // This code is contributed by akhilsaini
# Python3 program for the above approachimport math # Function to check whether# a number is prime or notdef isPrime(n):         # Corner cases    if (n <= 1):        return False    if (n <= 3):        return True     # If given number is a    # multiple of 2 or 3    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 # Function to check if sum# of count of digits of all# array elements is prime or notdef CheckSumPrime(A, N):         # Initialize sum with 0    sum = 0     # Traverse over the array    for i in range(0, N):                 # Convert array element to string        s = str(A[i])         # Add the count of        # digits to sum        sum += len(s)     # Print the result    if (isPrime(sum) == True):        print("Yes")    else:        print("No") # Drive Codeif __name__ == '__main__':         A = [ 1, 11, 12 ]     N = len(A)     # Function call    CheckSumPrime(A, N) # This code is contributed by akhilsaini
// C# program for the above approachusing System; class GFG{ // Function to check whether// a number is prime or notstatic bool isPrime(int n){         // Corner cases    if (n <= 1)        return false;    if (n <= 3)        return true;     // If given number is a    // multiple of 2 or 3    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;} // Function to check if sum// of count of digits of all// array elements is prime or notstatic void CheckSumPrime(int[] A, int N){         // Initialize sum with 0    int sum = 0;     // Traverse over the array    for(int i = 0; i < N; i++)    {                 // Convert array element to string        String s = A[i].ToString();         // Add the count of        // digits to sum        sum += s.Length;    }     // Print the result    if (isPrime(sum) == true)    {        Console.WriteLine("Yes");    }    else    {        Console.WriteLine("No");    }} // Drive Codepublic static void Main(){    int[] A = { 1, 11, 12 };     int N = A.Length;     // Function call    CheckSumPrime(A, N);}} // This code is contributed by akhilsaini
<script> // Javascript program for the above approach // Function to check whether// a number is prime or notfunction isPrime(n){    // Corner cases    if (n <= 1)        return false;    if (n <= 3)        return true;     // If given number is a    // multiple of 2 or 3    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 to check if sum// of count of digits of all// array elements is prime or notfunction CheckSumPrime(A, N){    // Initialize sum with 0    let sum = 0;     // Traverse over the array    for (let i = 0; i < N; i++) {         // Convert array element to string        let s = new String(A[i]);         // Add the count of        // digits to sum        sum += s.length;    }     // Print the result    if (isPrime(sum)) {         document.write("Yes" + "<br>");    }    else {         document.write("No" + "<br>");    }} // Drive Code    let A = [ 1, 11, 12 ];     let N = A.length     // Function call    CheckSumPrime(A, N);     // This code is contributed by gfgking     </script>
Yes
 
Time Complexity: O(N3/2)Auxiliary Space: O(1)
 
akhilsaini
gfgking
array-traversal-question
number-digits
Prime Number
Arrays
Mathematical
Arrays
Mathematical
Prime Number
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Program to find sum of elements in a given array
Trapping Rain Water
Reversal algorithm for array rotation
Move all negative numbers to beginning and positive to end with constant extra space
Window Sliding Technique
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Program to find sum of elements in a given array