Skip to content
Related Articles

Related Articles

Count primes less than number formed by replacing digits of Array sum with prime count till the digit

Improve Article
Save Article
  • Last Updated : 29 Jun, 2022
Improve Article
Save Article

Given an array arr[] of size N having only positive elements, the task is to find the number of primes less than the number formed after following the below operations:

  • Add all elements of the given array, say sum
  • Replace every digit of the sum with the total number of prime numbers occurring between 0 and that digit.

Examples:

Input: N = 5, arr[] = {7, 12, 9, 27, 1}
Output: 11
Explanation: Sum of all element is 56. 
The prime numbers between [0, 5] and [0, 6] are 3, which are (2, 3, 5). 
So, the new number becomes 33. 
Now the total prime numbers between [0, 33] are11. 
So the final answer will be 11.

Input: N = 4, arr[] = {1, 2, 3, 4}
Output: 0

 

Algorithm: This is a simple implementation based problem. The idea is to perform the operations one by one as mentioned and finally count the number of primes.

Follow the steps mentioned below to solve the problem.

  • Find the total sum of the given array.
  • Convert the sum into a string, say S.
  • Iterate over the string and replace each character with the number of primes between 0 and that character.
  • Convert the newly formed string into an integer Y. 
  • Count the total number of prime between 0 to Y and return it.

Below is the implementation of the above approach.

C++




// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check prime
bool checkPrime(int numberToCheck)
{
    if (numberToCheck == 1
        || numberToCheck == 0) {
        return false;
    }
    for (int i = 2; i * i <= numberToCheck;
         i++) {
        if (numberToCheck % i == 0) {
            return false;
        }
    }
    return true;
}
 
// Function to calculate total prime numbers
// between 0 to r
int totalprime(int r)
{
    // Count the number of primes
    int count = 0;
    for (int i = r; i >= 0; i--) {
        count += checkPrime(i);
    }
    return count;
}
 
// Function to find required count of primes
int findNum(int arr[], int n)
{
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum += arr[i];
    }
 
    // Converting sum to string
    string s = to_string(sum);
 
    // Calculating total prime numbers:
    for (int i = 0; i < s.length(); i++) {
        s[i] = totalprime(s[i] - '0') + '0';
    }
 
    // Converting newly formed string s
    // to integer.
    int y = stoi(s);
    return totalprime(y);
}
 
// Driver's code
int main()
{
    int arr[] = { 7, 12, 9, 27, 1 };
    int N = 5, sum = 0;
 
    // Function call
    cout << findNum(arr, N);
    return 0;
}


Java




// Java code to implement the approach
import java.io.*;
import java.util.*;
 
class GFG
{
   
  // Function to check prime
  public static boolean checkPrime(int numberToCheck)
  {
    if (numberToCheck == 1 || numberToCheck == 0) {
      return false;
    }
    for (int i = 2; i * i <= numberToCheck; i++) {
      if (numberToCheck % i == 0) {
        return false;
      }
    }
    return true;
  }
 
  // Function to calculate total prime numbers
  // between 0 to r
  public static int totalprime(int r)
  {
     
    // Count the number of primes
    int count = 0;
    for (int i = r; i >= 0; i--) {
      if (checkPrime(i) == true)
        count += 1;
    }
    return count;
  }
 
  // Function to find required count of primes
  public static int findNum(int arr[], int n)
  {
    int sum = 0;
    for (int i = 0; i < n; i++) {
      sum += arr[i];
    }
 
    // Converting sum to string
    StringBuilder s
      = new StringBuilder(Integer.toString(sum));
 
    // Calculating total prime numbers:
    for (int i = 0; i < s.length(); i++) {
      s.setCharAt(i,
                  (char)(totalprime(s.charAt(i) - '0')
                         + '0'));
    }
 
    // Converting newly formed string s
    // to integer.
    int y = Integer.parseInt(s.toString());
    return totalprime(y);
  }
  public static void main(String[] args)
  {
    int arr[] = { 7, 12, 9, 27, 1 };
    int N = 5, sum = 0;
 
    // Function call
    System.out.print(findNum(arr, N));
  }
}
 
// This code is contributed by Rohit Pradhan


Python3




# Python code to implement the approach
 
# Function to check prime
def checkPrime(numberToCheck):
    if (numberToCheck == 1 or numberToCheck == 0):
        return False
    i = 2
    while(i * i <= numberToCheck):
        if (numberToCheck % i == 0):
            return False
        i += 1
    return True
 
# Function to calculate total prime numbers
# between 0 to r
def totalprime(r):
   
    # Count the number of primes
    count = 0
    for i in range(r,-1,-1):
        count += checkPrime(i)
    return count
 
# Function to find required count of primes
def findNum(arr, n):
    sum = 0
    for i in range(n):
        sum += arr[i]
 
    # Converting sum to string
    s = str(sum)
 
    # Calculating total prime numbers:
    for i in range(len(s)):
        s = s.replace(s[i],chr(totalprime(ord(s[i]) - ord('0')) + ord('0')))
 
    # Converting newly formed string s
    # to integer.
    y = int(s)
    return totalprime(y)
 
# Driver's code
arr = [ 7, 12, 9, 27, 1 ]
N,sum = 5,0
 
# Function call
print(findNum(arr, N))
 
# This code is contributed by shinjanpatra


C#




// C# code to implement the approach
using System;
 
public class GFG
{
   
  // Function to check prime
  public static bool checkPrime(int numberToCheck)
  {
    if (numberToCheck == 1 || numberToCheck == 0) {
      return false;
    }
    for (int i = 2; i * i <= numberToCheck; i++) {
      if (numberToCheck % i == 0) {
        return false;
      }
    }
    return true;
  }
 
  // Function to calculate total prime numbers
  // between 0 to r
  public static int totalprime(int r)
  {
     
    // Count the number of primes
    int count = 0;
    for (int i = r; i >= 0; i--) {
      if (checkPrime(i) == true)
        count += 1;
    }
    return count;
  }
 
  // Function to find required count of primes
  public static int findNum(int []arr, int n)
  {
    int sum = 0;
    for (int i = 0; i < n; i++) {
      sum += arr[i];
    }
 
    // Converting sum to string
    String s
      = Convert. ToString(sum) ;
 
    // Calculating total prime numbers:
    for (int i = 0; i < s.Length; i++) {
        s = s.Substring(0,i)+(char)(totalprime(s[i] - '0')
                         + '0')+s.Substring(i+1);
    }
 
    // Converting newly formed string s
    // to integer.
    int y = Int32.Parse(s);
    return totalprime(y);
  }
  public static void Main(String[] args)
  {
    int []arr = { 7, 12, 9, 27, 1 };
    int N = 5, sum = 0;
 
    // Function call
    Console.Write(findNum(arr, N));
  }
}
 
// This code contributed by shikhasingrajput


Javascript




//JavaScript code to implement the approach
 
// Function to check prime
function checkPrime(numberToCheck)
{
    if (numberToCheck == 1 || numberToCheck == 0)
        return false;
    var i = 2;
    while(i * i <= numberToCheck)
    {
        if (numberToCheck % i == 0)
            return false;
        i += 1;
    }
    return true;
}
 
// Function to calculate total prime numbers
// between 0 to r
function totalprime(r)
{
   
    // Count the number of primes
    var count = 0;
    for (var i = r; i > -1; i--)
        count += checkPrime(i);
    return count;
}
 
 
// Function to find required count of primes
function findNum(arr, n)
{
    var sum = 0;
    for (var i = 0; i < n; i++)
        sum += arr[i];
 
    // Converting sum to string
    var s = sum.toString().split("");
 
    // Calculating total prime numbers:
    for (var i = 0; i < s.length; i++)
        s[i] = totalprime(Number(s[i]));
 
    // Converting newly formed string s
    // to integer.
    var y = Number(s.join(""));
    return totalprime(y);
}
 
// Driver's code
var arr = [ 7, 12, 9, 27, 1 ];
var N = 5;
 
// Function call
console.log(findNum(arr, N));
 
// This code is contributed by phasing17


Output

11

Time Complexity: O(N)
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!