My code is passing custom input and two test cases only.pls point out the mistake

#include
#include<math.h>
using namespace std;
int check(int n1);

int main()
{
string s;
int n,b[100],m,i,j,k,t[100];
int d,count;
int n1=0;
int count1=0;

cin>>n>>s;

for(i=0;i<n;i++)
{ if(d==1)
{
i=m+1;

}
for(j=i;j<n;j++)
{
count=(j-i)+1;
n1=0;
for(k=i;k<=j;k++)
{
b[k]=s[k]-‘0’;

		n1=n1+b[k]*pow(10,--count);
		
			
	}
	
	
d=check(n1);

if(d==1)
{    m=j;
   
   for(i=0;i<=count1;i++)
   {
   	 if(n1==t[i])
   	 {
   	 	
   	 	break;
   	 	
		}
		
		
   	
   }
   
   if(i==(count1+1))
   {
   	  count1++;
   	  t[count1]=n1;
   	  
   }
	 break;  		   
   }




}
	
	
}

cout<<count1;

return 0;

}

int check(int n1)
{ int flag=1;
int c[]={2,3,5,7,11,13,17,19,23,29};

if(n1==0)
{   
flag=0;
	return flag;
	
	
}

else if(n1==1)
{
	
	flag=0;
	return flag;
}


else
{

for(int i=0;i<10;i++)
{



if(n1==c[i])
{
	flag=1;
	break;
}

if((n1%c[i])==0)
{
flag=0;
break;

}


}

}
return flag;
}

Hello @apurvaraina99,

Please share your code using Coding Blocks online IDE.
The way you have shared it, introducing many syntax error to it.

Steps:

  1. Paste your code on IDE.
  2. Save your code.
  3. Share the URL with me.

Hello @S18ML0016,

There are following problems in your program:

  1. You have start from all the substring of length 1 and then increment the size of substring as you have to find the maximum CB numbers.

  2. You should keep track of the indexes that have been visited or traversed. This should be done to avoid using the elements that have been used in already detected CB number.

  3. The number you are sending to the check function be greater than the range of int as string is 16 digits long
    int check(long long int n1)

4.There are few more issues with your code:
int check(long long int n1)
{ int flag=1;
int c[]={2,3,5,7,11,13,17,19,23,29};

if(n1==0)
{   
flag=0;
	return flag;
	
	
}

else if(n1==1)
{
	
	flag=0;
	return flag;
}


else
{

for(int i=0;i<10;i++)
{



if(n1==c[i])
{
	flag=1;
    //modification
	return flag;
}

if((n1%c[i])==0)
{
flag=0;
//modification
return flag;

}


}

}
// if above conditions are not satisfied, then it is a CB number
flag=1;
return flag;

}

Correct Approach:

  1. check for all sub-strings of the given string of digits, starting from the all strings of length 1 and then gradually checking for the strings of increasing size:

    1.1. check if the sub-string is CB number: for this, create a function
    1.1.1. if sub-string is 1 or 0 return false.
    1.1.2. if sub-string is any of the {2,3,5,7,11,13,17,19,23,29}, then return true.
    1.1.3. if sub-string is divisible by any of the {2,3,5,7,11,13,17,19,23,29}, then return false.
    1.1.4. return false

    1.2. now if it is a substring:
    1.2.1. call a function that marks the index as visited for indexes which are part of that CB number.

    1.3. increment the count for CB number.

  2. Print the count

Hope, this would help.
Give a like, if you are satisfied.

1 Like

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.