Length of Smallest Non Prime Subsequence in given numeric String | C ++ program for the above approach ; Function to find the smallest length of resultant subsequence ; Check for a subsequence of size 1 ; Check for a subsequence of size 2 ; If none of the above check is successful then subsequence must be of size 3 ; Never executed ; Driver Code
#include <bits/stdc++.h> NEW_LINE 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 ; }