Maximum count of digits that can be removed such that remaining integer is consonant
Last Updated :
07 Mar, 2022
Given a string S representing an N digit integer, the task is to find the maximum number of digits that can be removed such that the remaining digits from a consonant integer.
Note that 0 and 1 are also considered as non-prime integers.
Example:
Input: S = “237”
Output: 1
Explanation: In the given integer, S[1] can be removed. Hence, S = “27”, which is the smallest possible integer that is non-prime. Therefore, the maximum number of digit that can be removed is 1.
Input: S = “35”
Output: -1
Explanation: It is not possible to create a non-prime integer using the above steps.
Approach: The given problem can be solved using the observation that all strings having 3 or more digits contain a subsequence of digits of length at most 2 such that the subsequence represents a non-prime integer. Using this observation, the given problem can be solved using the following steps:
- Create an array prime[], which stores whether a given integer is prime or not for all integers in the range [0, 100). This array can be efficiently created using the Sieve of Eratosthenes.
- Iterate the given string str[] and check if there exists any 1 digit string that is non-prime, i.e, {0, 1, 4, 6, 8, 9}.
- If no single-digit string exists, check if the length of the given string <= 2. In that case, if the integer represented by S is prime, return -1, otherwise return 0.
- Otherwise, return N – 2, which will be the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool prime[100];
void sieve()
{
for ( int i = 0; i < 100; i++) {
prime[i] = true ;
}
prime[0] = false ;
prime[1] = false ;
for ( int i = 2; i < 100; i++) {
if (!prime[i])
continue ;
for ( int j = 2 * i; j < 100; j += i) {
prime[j] = false ;
}
}
}
int maxCount(string S)
{
for ( int i = 0; i < S.size(); i++) {
if (!prime[S[i] - '0' ]) {
return S.length() - 1;
}
}
if (S.length() <= 2) {
if (prime[stoi(S)])
return -1;
else
return 0;
}
else {
return S.length() - 2;
}
}
int main()
{
string S = "237" ;
sieve();
cout << maxCount(S);
return 0;
}
|
Java
import java.util.*;
class GFG
{
private static boolean [] prime = new boolean [ 100 ];
public static void sieve()
{
for ( int i = 0 ; i < 100 ; i++) {
prime[i] = true ;
}
prime[ 0 ] = false ;
prime[ 1 ] = false ;
for ( int i = 2 ; i < 100 ; i++) {
if (!prime[i])
continue ;
for ( int j = 2 * i; j < 100 ; j += i) {
prime[j] = false ;
}
}
}
public static int maxCount(String S)
{
for ( int i = 0 ; i < S.length(); i++)
{
if (!prime[S.charAt(i) - '0' ]) {
return S.length() - 1 ;
}
}
if (S.length() <= 2 ) {
if (prime[Integer.parseInt(S)])
return - 1 ;
else
return 0 ;
}
else {
return S.length() - 2 ;
}
}
public static void main(String[] args)
{
String S = "237" ;
sieve();
System.out.print(maxCount(S));
}
}
|
Python3
prime = []
def sieve():
for i in range ( 100 ):
prime.append( True )
prime[ 0 ] = False
prime[ 1 ] = False
for i in range ( 2 , 100 ):
if ( not prime[i]):
continue
for j in range ( 2 * i, 100 , i):
prime[j] = False
def maxCount(S):
N = len (S)
for i in range (N):
if ( not prime[ int (S[i])]):
return N - 1
if (N < = 2 ):
if (prime[ int (S)]):
return - 1
else :
return 0
else :
return N - 2
S = "237"
sieve()
print (maxCount(S))
|
C#
using System;
public class GFG
{
private static bool [] prime = new bool [100];
public static void sieve()
{
for ( int i = 0; i < 100; i++) {
prime[i] = true ;
}
prime[0] = false ;
prime[1] = false ;
for ( int i = 2; i < 100; i++) {
if (!prime[i])
continue ;
for ( int j = 2 * i; j < 100; j += i) {
prime[j] = false ;
}
}
}
public static int maxCount( string S)
{
for ( int i = 0; i < S.Length; i++)
{
if (!prime[S[i] - '0' ]) {
return S.Length - 1;
}
}
if (S.Length <= 2) {
if (prime[Int32.Parse(S)])
return -1;
else
return 0;
}
else {
return S.Length - 2;
}
}
static public void Main (){
string S = "237" ;
sieve();
Console.Write(maxCount(S));
}
}
|
Javascript
<script>
let prime = new Array(100);
function sieve()
{
for (let i = 0; i < 100; i++) {
prime[i] = true ;
}
prime[0] = false ;
prime[1] = false ;
for (let i = 2; i < 100; i++) {
if (!prime[i])
continue ;
for (let j = 2 * i; j < 100; j += i) {
prime[j] = false ;
}
}
}
function maxCount(S)
{
for (let i = 0; i < S.length; i++)
{
if (!prime[S[i].charCodeAt(0) - '0' .charCodeAt(0)]) {
return S.length - 1;
}
}
if (S.length <= 2) {
if (prime[parseInt(S)])
return -1;
else
return 0;
}
else {
return S.length - 2;
}
}
let S = "237" ;
sieve();
document.write(maxCount(S));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...