# Length of Smallest Non Prime Subsequence in given numeric String

• Last Updated : 16 Sep, 2021

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++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to find the smallest``// length of resultant subsequence``void` `findMinimumSubsequence(``    ``string S)``{``    ``bool` `flag = ``false``;``    ``string dummy;` `    ``// Check for a subsequence of size 1``    ``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``;``        ``}``    ``}` `    ``// Check for a subsequence of size 2` `    ``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 none of the above check is``    ``// successful then subsequence``    ``// must be of size 3`` ` `    ``if` `(!flag)``    ``{``        ``if` `(S.length() >= 3)``        ``{` `            ``// Never executed``            ``cout << 3;``        ``}``        ``else``        ``{``            ``cout << -1;``        ``}``    ``}``}` `// Driver Code``int` `main()``{``    ``string S = ``"237"``;``    ``findMinimumSubsequence(S);``    ``return` `0;``}` `// This code is contributed by Potta Lokesh`

## Java

 `// Java program for the above approach` `import` `java.io.*;` `class` `GFG {` `    ``// Function to find the smallest``    ``// length of resultant subsequence``    ``public` `static` `void` `findMinimumSubsequence(``        ``String S)``    ``{``        ``boolean` `flag = ``false``;``        ``StringBuilder dummy = ``new` `StringBuilder();` `        ``// Check for a subsequence of size 1``        ``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``;``            ``}``        ``}` `        ``// Check for a subsequence of size 2``        ``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 none of the above check is``        ``// successful then subsequence``        ``// must be of size 3``        ``if` `(!flag) {``            ``if` `(S.length() >= ``3``) {` `                ``// Never executed``                ``System.out.println(``3``);``            ``}``            ``else` `{``                ``System.out.println(-``1``);``            ``}``        ``}``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``String S = ``"237"``;``        ``findMinimumSubsequence(S);``    ``}``}`

## C#

 `// C# program for the above approach``using` `System;``class` `GFG``{``  ` `    ``// Function to find the smallest``    ``// length of resultant subsequence``    ``static` `void` `findMinimumSubsequence(``string` `S)``    ``{``        ``bool` `flag = ``false``;``        ``string` `dummy = ``""``;` `        ``// Check for a subsequence of size 1``        ``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``;``            ``}``        ``}` `        ``// Check for a subsequence of size 2``        ``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 none of the above check is``        ``// successful then subsequence``        ``// must be of size 3` `        ``if` `(flag == ``false``) {``            ``if` `(S.Length >= 3) {` `                ``// Never executed``                ``Console.WriteLine(3);``            ``}``            ``else` `{``                ``Console.WriteLine(-1);``            ``}``        ``}``    ``}` `    ``// Driver Code``    ``public` `static` `void` `Main()``    ``{``        ``string` `S = ``"237"``;``        ``findMinimumSubsequence(S);``    ``}``}` `// This code is contributed by ukasp.`

## Python3

 `# Python 3 program for the above approach` `# Function to find the smallest``# length of resultant subsequence``def` `findMinimumSubsequence(S):``    ``flag ``=` `False``    ``dummy ``=` `''` `    ``# Check for a subsequence of size 1``    ``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` `    ``# Check for a subsequence of size 2``    ``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 none of the above check is``    ``# successful then subsequence``    ``# must be of size 3``    ``if` `(flag ``=``=` `False``):``        ``if` `(``len``(S) >``=` `3``):``            ``# Never executed``            ``print``(``3``)``        ``else``:``            ``print``(``-``1``)``    ` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ``S ``=` `"237"``    ``findMinimumSubsequence(S)``    ` `    ``# This code is contributed by ipg2016107.`

## Javascript

 ``
Output:
`2`

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

My Personal Notes arrow_drop_up