Given a string S of size N consisting of digits [1, 9], the task is to find the length of the smallest subsequence in the string such that it is not a prime number.
Examples:
Input: S = “237”
Output: 2
Explanation:
There are 7 non empty subsequence {“2”, “3”, “7”, “23”, “27”, “37”, “237”}. Among these subsequence there are two subsequence that are not prime, i.e., 27 and 237. Thus as 27 has a length of 2. So print 2.
Input: S = “44444”
Output: 1
Approach: The idea to solve this problem is based on the observation that if on deleting j or more than j characters from the string S the string is a prime number, then the answer should be greater than j. Based on this fact, form all the strings such that deleting element from that string gives a prime number. All the possible strings from the above intuition are {2, 3, 5, 7, 23, 37, 53, 73} Thus the maximum possible size of the sub-sequence is 3. Therefore, the idea is to traverse over all the subsequences of size 1 and size 2 and if the subsequences are found to contain at least 1 element which is not present in the list then the size might be either 1 or 2. Otherwise, the size will be 3. Follow the steps below to solve the problem:
- Initialize the boolean variable flag as false.
- Initialize an empty string dummy.
- Iterate over the range [0, N) using the variable j and perform the following tasks:
- If the character at j-th position is not equal to 2 or 3 or 5 or 7, then print the answer as 1, set the value of flag as true and break.
- If flag is true, then return otherwise perform the following tasks.
- Iterate over the range [0, N) using the variable j and perform the following tasks:
- Iterate over the range [j+1, N) using the variable j1 and perform the following tasks:
- Build a dummy string with characters at j and j1 position.
- If the dummy string is not equal to 2, 3, 5, 7, 23, 37, 53, and 73 then print the answer as 2 and set the value of flag as true and break.
- If the flag is false and the length of the string S is greater than equal to 3, then print 3 as the answer else print -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findMinimumSubsequence(
string S)
{
bool flag = false ;
string dummy;
for ( int j = 0; j < S.length(); j++)
{
if (S[j] != '2' && S[j] != '3' && S[j] != '5' && S[j] != '7' )
{
cout << 1;
flag = true ;
break ;
}
}
if (!flag)
{
for ( int j = 0;
j < S.length() - 1; j++)
{
for ( int j1 = j + 1;
j1 < S.length(); j1++)
{
dummy = S[j] + S[j1];
if (dummy != "23" && dummy != "37" && dummy != "53" && dummy != "73" )
{
cout << 2;
}
if (flag = true )
break ;
}
if (flag = true )
break ;
}
}
if (!flag)
{
if (S.length() >= 3)
{
cout << 3;
}
else
{
cout << -1;
}
}
}
int main()
{
string S = "237" ;
findMinimumSubsequence(S);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void findMinimumSubsequence(
String S)
{
boolean flag = false ;
StringBuilder dummy = new StringBuilder();
for ( int j = 0 ; j < S.length(); j++) {
if (S.charAt(j) != '2' && S.charAt(j) != '3'
&& S.charAt(j) != '5'
&& S.charAt(j) != '7' ) {
System.out.println( 1 );
flag = true ;
break ;
}
}
if (!flag) {
loop:
for ( int j = 0 ;
j < S.length() - 1 ; j++) {
for ( int j1 = j + 1 ;
j1 < S.length(); j1++) {
dummy = new StringBuilder(
Character.toString(S.charAt(j)));
dummy.append(S.charAt(j1));
if (!dummy.toString().equals( "23" )
&& !dummy.toString().equals( "37" )
&& !dummy.toString().equals( "53" )
&& !dummy.toString().equals( "73" )) {
System.out.println( 2 );
flag = true ;
break loop;
}
}
}
}
if (!flag) {
if (S.length() >= 3 ) {
System.out.println( 3 );
}
else {
System.out.println(- 1 );
}
}
}
public static void main(String[] args)
{
String S = "237" ;
findMinimumSubsequence(S);
}
}
|
C#
using System;
class GFG
{
static void findMinimumSubsequence( string S)
{
bool flag = false ;
string dummy = "" ;
for ( int j = 0; j < S.Length; j++) {
if (S[j] != '2' && S[j] != '3' && S[j] != '5'
&& S[j] != '7' ) {
Console.WriteLine(1);
flag = true ;
break ;
}
}
if (!flag) {
for ( int j = 0; j < S.Length - 1; j++) {
for ( int j1 = j + 1; j1 < S.Length; j1++) {
dummy = S[j].ToString()
+ S[j1].ToString();
if (dummy != "23" && dummy != "37"
&& dummy != "53" && dummy != "73" ) {
Console.WriteLine(2);
}
if (flag == true )
break ;
else
flag = true ;
}
if (flag == true )
break ;
}
}
if (flag == false ) {
if (S.Length >= 3) {
Console.WriteLine(3);
}
else {
Console.WriteLine(-1);
}
}
}
public static void Main()
{
string S = "237" ;
findMinimumSubsequence(S);
}
}
|
Python3
def findMinimumSubsequence(S):
flag = False
dummy = ''
for j in range ( len (S)):
if (S[j] ! = '2' and S[j] ! = '3' and S[j] ! = '5' and S[j] ! = '7' ):
print ( 1 )
flag = True
break
if (flag = = False ):
for j in range ( len (S)):
for j1 in range (j + 1 , len (S), 1 ):
dummy = S[j] + S[j1]
if (dummy ! = "23" and dummy ! = "37" and dummy ! = "53" and dummy ! = "73" ):
print ( 2 )
if (flag = = True ):
break
else :
flag = True
if (flag = = True ):
break
if (flag = = False ):
if ( len (S) > = 3 ):
print ( 3 )
else :
print ( - 1 )
if __name__ = = '__main__' :
S = "237"
findMinimumSubsequence(S)
|
Javascript
<script>
function findMinimumSubsequence(S)
{
let flag = false ;
for (let j = 0; j < S.length; j++)
{
if (S[j] != '2' && S[j] != '3' && S[j] != '5' && S[j] != '7' )
{
document.write(1);
flag = true ;
break ;
}
}
if (flag == false )
{
for (let j = 0; j < S.length; j++)
{
for (let j1 = j + 1; j1 < S.length; j1++)
{
let dummy = S[j] + S[j1];
if (dummy != "23" && dummy != "37" && dummy != "53" && dummy != "73" )
{
document.write(2);
}
if (flag == true )
break ;
else
flag = true ;
}
if (flag == true )
break ;
}
}
if (flag == false )
{
if (S.length >= 3)
{
document.write(3);
}
else
{
document.write(-1);
}
}
}
let S = "237" ;
findMinimumSubsequence(S);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)