The Wayback Machine - https://web.archive.org/web/20210910043656/https://www.geeksforgeeks.org/length-of-smallest-non-prime-subsequence-in-given-numeric-string/
Skip to content
Related Articles

Related Articles

Improve Article

Length of Smallest Non Prime Subsequence in given numeric String

  • Last Updated : 09 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 <bits/stdc++.h>
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);
    }
}
Output: 
2

 

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :