Skip to content
Related Articles

Related Articles

Improve Article
Count strings having sum of ASCII values of characters equal to a Prime or Armstrong Number
  • Difficulty Level : Expert
  • Last Updated : 06 Jun, 2021

Given an array arr[] of size N containing strings, the task is to count the number of strings having sum of ASCII values of characters equal to an Armstrong Number number or a Prime Number.

Examples:

Input: arr[] = {“hello”, “nace”}
Output:
Number of Armstrong Strings are: 1
Number of Prime Strings are: 0
Explanation: Sum of ASCII values of characters of each string is: {532, 407}, out of which 407 is an Armstrong Number, and none of them is a Prime Number.
Hence, the armstrong valued string is “nace”.

Input: arr[] = {“geeksforgeeks”, “a”, “computer”, “science”, “portal”, “for”, “geeks”}
Output:
Number of Armstrong Strings are: 0
Number of Prime Strings are: 2 
Explanation: Sum of ASCII values of characters of each string is: {1381, 97, 879, 730, 658, 327, 527}, out of which 1381 and 97 are Prime Numbers, and none of them is an Armstrong Number.
Hence, prime valued strings are “geeksforgeeks” and “a”.

Approach: This problem can be solved by calculating the ASCII value of each string. Follow the steps below to solve this problem:

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if a
// number is prime number
bool isPrime(int num)
{
     
    // Define a flag variable
    bool flag = false;
 
    if (num > 1)
    {
         
        // Check for factors of num
        for(int i = 2; i < num; i++)
        {
             
            // If factor is found,
            // set flag to True and
            // break out of loop
            if ((num % i) == 0)
            {
                flag = true;
                break;
            }
        }
    }
 
    // Check if flag is True
    if (flag)
        return false;
    else
        return true;
}
 
// Function to calculate
// order of the number x
int order(int x)
{
    int n = 0;
    while (x != 0)
    {
        n = n + 1;
        x = x / 10;
    }
    return n;
}
 
// Function to check whether the given
// number is Armstrong number or not
bool isArmstrong(int x)
{
    int n = order(x);
    int temp = x;
    int sum1 = 0;
 
    while (temp != 0)
    {
        int r = temp % 10;
        sum1 = sum1 + pow(r, n);
        temp = temp / 10;
    }
     
    // If the condition satisfies
    return (sum1 == x);
}
 
// Function to count
// Armstrong valued strings
int count_armstrong(vector<string> li)
{
     
    // Stores the count of
    // Armstrong valued strings
    int c = 0;
 
    // Iterate over the list
    for(string ele : li)
    {
         
        // Store the value
        // of the string
        int val = 0;
 
        // Find value of the string
        for(char che:ele)
            val += che;
 
        // Check if it an Armstrong number
        if (isArmstrong(val))
            c += 1;
    }
    return c;
}
 
// Function to count
// prime valued strings
int count_prime(vector<string> li)
{
     
    // Store the count of
    // prime valued strings
    int c = 0;
 
    // Iterate over the list
    for(string ele:li)
    {
         
        // Store the value
        // of the string
        int val = 0;
 
        // Find value of the string
        for(char che : ele)
            val += che;
 
        // Check if it
        // is a Prime Number
        if (isPrime(val))
            c += 1;
    }
    return c;
}
 
// Driver code
int main()
{
    vector<string> arr = { "geeksforgeeks", "a", "computer",
                           "science", "portal", "for", "geeks"};
     
    // Function Call
    cout << "Number of Armstrong Strings are: "
         << count_armstrong(arr) << endl;
    cout << "Number of Prime Strings are: "
         << count_prime(arr) << endl;
}
 
// This code is contributed by mohit kumar 29

Python3




# Python program for the above approach
 
# Function to check if a
# number is prime number
def isPrime(num):
 
    # Define a flag variable
    flag = False
 
    if num > 1:
 
        # Check for factors of num
        for i in range(2, num):
 
            # If factor is found,
            # set flag to True and
            # break out of loop
            if (num % i) == 0:
                flag = True
                break
 
    # Check if flag is True
    if flag:
        return False
    else:
        return True
 
# Function to calculate
# order of the number x
def order(x):
    n = 0
    while (x != 0):
        n = n + 1
        x = x // 10
    return n
 
# Function to check whether the given
# number is Armstrong number or not
def isArmstrong(x):
    n = order(x)
    temp = x
    sum1 = 0
 
    while (temp != 0):
 
        r = temp % 10
        sum1 = sum1 + r**n
        temp = temp // 10
 
    # If the condition satisfies
    return (sum1 == x)
 
# Function to count
# Armstrong valued strings
def count_armstrong(li):
 
    # Stores the count of
    # Armstrong valued strings
    c = 0
 
    # Iterate over the list
    for ele in li:
 
        # Store the value
        # of the string
        val = 0
         
        # Find value of the string
        for che in ele:
            val += ord(che)
             
        # Check if it an Armstrong number
        if isArmstrong(val):
            c += 1
    return c
 
# Function to count
# prime valued strings
def count_prime(li):
     
    # Store the count of
    # prime valued strings
    c = 0
     
    # Iterate over the list
    for ele in li:
       
        # Store the value
        # of the string
        val = 0
         
        # Find value of the string
        for che in ele:
            val += ord(che)
             
        # Check if it
        # is a Prime Number
        if isPrime(val):
            c += 1
    return c
 
 
# Driver code
arr = ["geeksforgeeks", "a", "computer",
       "science", "portal", "for", "geeks"]
 
# Function Call
print("Number of Armstrong Strings are:", count_armstrong(arr))
print("Number of Prime Strings are:", count_prime(arr))

Time Complexity: O(N*M), where M is the length of the longest string in the array arr[]
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :