Sir my code considering 8161 as cb number i dont know why please check and make correction in it

#include<bits/stdc++.h>
using namespace std;
int convertToInt(string str)
{
int ans=0;
int p=1;
for(int i=str.size()-1;i>=0;i–)
{
ans +=((str[i])-‘0’)*p;
p *=10;
}
return ans;
}
bool cbnumber(string str){
int a[]={2,3,5,7,11,13,17,19,23,29};
int n=10;
int k = convertToInt(str);
//cout<<" integer—"<<k;
if(k==0||k==1)
{
return false;
}
for(int i=0;i<n;i++){
if ((k==a[i])){
return true;
}
}
for(int i=0;i<n;i++){
if(k%a[i]==0){
return false;
}
}
for(int i=0;i<n;i++){
if(k%a[i]!=0){
return true;
}
}
return false;
}
int subString(string s, int n)
{
int count=0;
for (int i = 0; i < n; i++)
for (int len = 1; len <= n - i; len++)
{
if(cbnumber(s.substr(i,len))){
count++;
}
}
return count;
}

int main()
{
int n;
cin>>n;
cin.get();
string s ;
getline(cin,s);
int count =subString(s,s.length());
cout<<count<<endl;
return 0;
}

@Harshit-Kumar-2577121455897279 Please provide your code in Coding Blocks IDE.

here it is

@Harshit-Kumar-2577121455897279 Instead of creating all the CB numbers possible from i.
Find all the CB number of len 1, then of len 2 then len 3 and so on.
Also make a visited array that will tell you which element is taken, to prevent considering same position in multiple numbers
If this resolves your doubt mark it as resolved.

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.

1 Like