Count primes less than number formed by replacing digits of Array sum with prime count till the digit
Last Updated :
16 May, 2023
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++
#include <bits/stdc++.h>
using namespace std;
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 ;
}
int totalprime( int r)
{
int count = 0;
for ( int i = r; i >= 0; i--) {
count += checkPrime(i);
}
return count;
}
int findNum( int arr[], int n)
{
int sum = 0;
for ( int i = 0; i < n; i++) {
sum += arr[i];
}
string s = to_string(sum);
for ( int i = 0; i < s.length(); i++) {
s[i] = totalprime(s[i] - '0' ) + '0' ;
}
int y = stoi(s);
return totalprime(y);
}
int main()
{
int arr[] = { 7, 12, 9, 27, 1 };
int N = 5, sum = 0;
cout << findNum(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
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 ;
}
public static int totalprime( int r)
{
int count = 0 ;
for ( int i = r; i >= 0 ; i--) {
if (checkPrime(i) == true )
count += 1 ;
}
return count;
}
public static int findNum( int arr[], int n)
{
int sum = 0 ;
for ( int i = 0 ; i < n; i++) {
sum += arr[i];
}
StringBuilder s
= new StringBuilder(Integer.toString(sum));
for ( int i = 0 ; i < s.length(); i++) {
s.setCharAt(i,
( char )(totalprime(s.charAt(i) - '0' )
+ '0' ));
}
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 ;
System.out.print(findNum(arr, N));
}
}
|
Python3
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
def totalprime(r):
count = 0
for i in range (r, - 1 , - 1 ):
count + = checkPrime(i)
return count
def findNum(arr, n):
sum = 0
for i in range (n):
sum + = arr[i]
s = str ( sum )
for i in range ( len (s)):
s = s.replace(s[i], chr (totalprime( ord (s[i]) - ord ( '0' )) + ord ( '0' )))
y = int (s)
return totalprime(y)
arr = [ 7 , 12 , 9 , 27 , 1 ]
N, sum = 5 , 0
print (findNum(arr, N))
|
C#
using System;
public class GFG
{
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 ;
}
public static int totalprime( int r)
{
int count = 0;
for ( int i = r; i >= 0; i--) {
if (checkPrime(i) == true )
count += 1;
}
return count;
}
public static int findNum( int []arr, int n)
{
int sum = 0;
for ( int i = 0; i < n; i++) {
sum += arr[i];
}
String s
= Convert. ToString(sum) ;
for ( int i = 0; i < s.Length; i++) {
s = s.Substring(0,i)+( char )(totalprime(s[i] - '0' )
+ '0' )+s.Substring(i+1);
}
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;
Console.Write(findNum(arr, N));
}
}
|
Javascript
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 totalprime(r)
{
var count = 0;
for ( var i = r; i > -1; i--)
count += checkPrime(i);
return count;
}
function findNum(arr, n)
{
var sum = 0;
for ( var i = 0; i < n; i++)
sum += arr[i];
var s = sum.toString().split( "" );
for ( var i = 0; i < s.length; i++)
s[i] = totalprime(Number(s[i]));
var y = Number(s.join( "" ));
return totalprime(y);
}
var arr = [ 7, 12, 9, 27, 1 ];
var N = 5;
console.log(findNum(arr, N));
|
Time Complexity: O(N*sqrt(R))
Auxiliary Space: O(log(R))
Share your thoughts in the comments
Please Login to comment...