i did not understand the problem and the approach behimd it
Problem in understanding the problem
Hello @goelsparsh277,
In this question you have to find how many CB numbers you can generate from a given number.
Description of CB numbers are given in the question.
Approach:
-
check for all sub-strings of the given string of digits, starting from 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. -
Print the count
Hope, this would help.
If you still have any issue, feel free to ask.
Give a like if you are satisfied.
#include #include #include<math.h> #include using namespace std; bool iscb(long long int a){ if(a==1){ return false; } if(a==0){ return false; } if(a==2){ return true; } for(int i=2;i<=sqrt(a);i++){ if(a%i==0){ return false; } } return true; } int main() { int n,count=0; cin>>n; string s; cin>>s; for(int i=0;i<s.size();i++){ for(int j=1;j<=s.size()-i;j++){ string ne=s.substr(i,j); stringstream geek(ne); long long int x; geek>>x; if(iscb(x)){ cout<<x<<" "; count++; i=j-1+i; break; } } } cout<<count; return 0; }
i cant find the prolem
Hello @goelsparsh277,
Mistakes in your code:
-
There is no code for the point 1.1.2. of my previous reply.
You are returning true only for a=2 in that point. -
Missing logic for point 1.2.1.
Reason to use it:
To avoid using the element present on the same index in two different CB numbers. -
You have to first check for all sub-strings of length 1 and then increment the size one by one of the sub-strings.
Reason:
You have to maximize the count for CB number.
Corrected code:
Hope, this would help.
Give a like if you are satisfied.
okay got it , thank you sir