Whats rong in my code not passing all test cases

#include<iostream>
#include<string>
using namespace std;
bool checkcb(long long sub)
{
  if (sub==0||sub==1)
  {
      return false;
  }
  int a[]={2,3,5,7,11,13,17,19,23,29};
  for (int i = 0; i < sizeof(a)/sizeof(int); i++)
  {
      if (a[i]==sub)
      {
          return true;
      }
     
      
  }
  for (int i = 0; i <  sizeof(a)/sizeof(int); i++)
  {
      if (sub%a[i]==0)
      {
          return false;
      }
      
  }
  
  return true;



}
bool isvalid(int start,int end,bool visited[])
{
for (start; start<end; start++)
{
    if (visited[start]==true)
    {
        return false;
        /* code */
    }
    
}


return true;


}
int main(){
     int n;
     cin>>n;
    string s;
    cin>>s;
    int ans=0;
    bool visited[17]={false};
    for (int i = 0; i <=s.length()-1; i++)
    {
        for (int j = 1; j <=s.length()-i; j++)
        {
            string sub=s.substr(i,j);
         
             if (checkcb(stoll(sub))&&isvalid(i,i+j,visited))
            {
                ans++;
                for (int k = i; k<i+j; k++)
                {
                    visited[k]=true;
                }
            
            } 
            
        }
    }
    cout<<ans;
    return 0;
}

hello @sagar_aggarwal

there is small logical .
what u r doing is from index i u r iterating all possible substring startng from i. this will not give maximum.

the optimal way is to first find all one length cb number then all 2 length cb number ,then all 3 length cb no and so on (why small lenght to big lenght ? it is becuase we want to maximize count of cb number which can be possible by picking small lenght cb number first).

image

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.