Given an array arr[] of size N containing strings, the task is to count the number of strings having the sum of ASCII (American Standard Code for Information Interchange) 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:
- Initialize two variables, countPrime and countArmstrong as 0, to store the count of Prime and Armstrong valued strings.
-
Iterate over the range of indices [0, N – 1] using a variable, say i and perform the following steps:
- Store the sum of ASCII values of characters of the current string arr[i] in a variable, say val.
- If the number val is an Armstrong Number, increment countArmstrong by 1.
- If the number val is a Prime Number, increment countPrime by 1.
- Print the values of countPrime and countArmstrong as the result.
Below is the implementation of the above approach:
// 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 |
// Java program for the above approach import java.io.*;
class GFG {
// Function to check if a
// number is prime number
static boolean isPrime( int num)
{
// Define a flag variable
boolean 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
static 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
static boolean isArmstrong( int x)
{
int n = order(x);
int temp = x;
int sum1 = 0 ;
while (temp != 0 ) {
int r = temp % 10 ;
sum1 = sum1 + ( int )(Math.pow(r, n));
temp = temp / 10 ;
}
// If the condition satisfies
return (sum1 == x);
}
// Function to count
// Armstrong valued strings
static int count_armstrong(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.toCharArray()) val += che;
// Check if it an Armstrong number
if (isArmstrong(val))
c += 1 ;
}
return c;
}
// Function to count
// prime valued strings
static int count_prime(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.toCharArray()) val += che;
// Check if it
// is a Prime Number
if (isPrime(val))
c += 1 ;
}
return c;
}
// Driver code
public static void main (String[] args) {
String[] arr
= { "geeksforgeeks" , "a" , "computer" ,
"science" , "portal" , "for" ,
"geeks" };
// Function Call
System.out.println(
"Number of Armstrong Strings are: "
+ count_armstrong(arr));
System.out.println( "Number of Prime Strings are: "
+ count_prime(arr));
}
} // This code is contributed by patel2127. |
# 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))
|
// C# program for the above approach using System;
using System.Collections.Generic;
class GFG
{ // Function to check if a
// number is prime number
static 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
static 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
static bool isArmstrong( int x)
{
int n = order(x);
int temp = x;
int sum1 = 0;
while (temp != 0) {
int r = temp % 10;
sum1 = sum1 + ( int )(Math.Pow(r, n));
temp = temp / 10;
}
// If the condition satisfies
return (sum1 == x);
}
// Function to count
// Armstrong valued strings
static int count_armstrong( string [] li)
{
// Stores the count of
// Armstrong valued strings
int c = 0;
// Iterate over the list
foreach ( string ele in li)
{
// Store the value
// of the string
int val = 0;
// Find value of the string
foreach ( char che in ele) val += che;
// Check if it an Armstrong number
if (isArmstrong(val))
c += 1;
}
return c;
}
// Function to count
// prime valued strings
static int count_prime( string [] li)
{
// Store the count of
// prime valued strings
int c = 0;
// Iterate over the list
foreach ( string ele in li)
{
// Store the value
// of the string
int val = 0;
// Find value of the string
foreach ( char che in ele) val += che;
// Check if it
// is a Prime Number
if (isPrime(val))
c += 1;
}
return c;
}
// Driver code
public static void Main()
{
string [] arr
= { "geeksforgeeks" , "a" , "computer" ,
"science" , "portal" , "for" ,
"geeks" };
// Function Call
Console.WriteLine(
"Number of Armstrong Strings are: "
+ count_armstrong(arr));
Console.WriteLine( "Number of Prime Strings are: "
+ count_prime(arr));
}
} // This code is contributed by ukasp. |
<script> // JavaScript program for the above approach // Function to check if a // number is prime number function isPrime(num) {
// Define a flag variable
let flag = false ;
if (num > 1) {
// Check for factors of num
for (let 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 function order(x) {
let 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 function isArmstrong(x) {
let n = order(x);
let temp = x;
let sum1 = 0;
while (temp != 0) {
let r = temp % 10;
sum1 = sum1 + Math.pow(r, n);
temp = temp / 10;
}
// If the condition satisfies
return (sum1 == x);
} // Function to count // Armstrong valued strings function count_armstrong(li) {
// Stores the count of
// Armstrong valued strings
let c = 0;
// Iterate over the list
for (let ele of li) {
// Store the value
// of the string
let val = 0;
// Find value of the string
for (let che of ele)
val += che.charCodeAt(0);
// Check if it an Armstrong number
if (isArmstrong(val))
c += 1;
}
return c;
} // Function to count // prime valued strings function count_prime(li) {
// Store the count of
// prime valued strings
let c = 0;
// Iterate over the list
for (let ele of li) {
// Store the value
// of the string
let val = 0;
// Find value of the string
for (let che of ele)
val += che.charCodeAt(0);
// Check if it
// is a Prime Number
if (isPrime(val))
c += 1;
}
return c;
} // Driver code let arr = [ "geeksforgeeks" , "a" , "computer" ,
"science" , "portal" , "for" , "geeks" ];
// Function Call document.write( "Number of Armstrong Strings are: "
+ count_armstrong(arr) + "<br>" );
document.write( "Number of Prime Strings are: "
+ count_prime(arr) + "<br>" );
// This code is contributed by gfgking </script> |
Number of Armstrong Strings are: 0 Number of Prime Strings are: 2
Time Complexity: O(N*M), where M is the length of the longest string in the array arr[]
Auxiliary Space: O(1)