I am not able to solve FINDING CB NUMBER I have written this code

Hello @dsingh200021,

Where are you checking for the following conditions:

  1. 0 and 1 are not a CB number.
  2. 2,3,5,7,11,13,17,19,23,29 are CB numbers.
  3. Any number not divisible by the numbers in point 2( Given above) are also CB numbers.

And please, explain the logic of your code.

How to approach this question, please give me just hint to solve.
I have wasted an hour on this question

Sure,

  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.
If you still face issue, feel free to ask.
Give a like, if you are satisfied.

2 Likes

I am not able to check using my check function

d=atoi(b.c_str());
This wont help. It is for converting a character to integer.

Even if you’ll use stoi() function, it wont run correctly as the input string can be of 17 characters long and this function only returns the integer value, not the long.

Solution: use a loop and write your code to convert a string to number and store result in a variable of long data type.

How can I convert a string to integer using a loop?

long num =0;
int n=sub_string.length();
int p=1;
for(i =n-1; i>=0;i- -){
num=num+s[i]p;
p=p
10;
}

Hope, this would help.

This logic is not working I can’t generate integers from strings,see this code

This is because a[i] is a char and when you multiply it with p,
then in actual, it’s ASCII values is being multiplied by p.

See this:

My question is getting WRONG answer for two tests case
My program is getting correct answer for three test cases,and two run error
Please check this code its working for 4991 and 81615 as input

  1. Why aren’t you considering all the CB numbers given in the question?
    int s[10]={2,3,5,7,11,13,17}; this is incomplete.

  2. 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.
    for(k=j;k<=j;k++) //modify this.

  3. The following logic of valid is not satisfying all the testcases.
    string a;
    bool valid(string str){ //Function to check if the substring already there
    int i,j;
    for(i=0;str[i]!=’\0’;++i){
    for(j=0;a[j]!=’\0’;++j){
    if(str[i]==a[j])
    return false;
    }
    }
    a=str;
    return true;
    }
    You are just checking with the last CB number , which you are storing in a.
    You are requiored to keep track of the indeces.

  4. How are you so sure that the substring would be at max 10 characters long?
    char str[10]={0};

I have modified your code:

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


Hey @S18ML0016 VIrender SIngh can you please explain how this statement is working in for loop . I am a bit perplexed .Help!!

Hello @LPLC0156,

You have pointed out a very interesting thing.
j<b[j]!=’\0’

To understand the execution flow of the above statement, you have to consider precedence of operators.
The order of precedence of operators in the above statement is:
[ ]
<
!=

To, understand it better, run the following code:

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

1 Like