Finding CB Numbers

What’s wrong in my code ?

Hello @vaishnavtannu,

The recursive approach you have used will always print a single integer which may or may not be a correct CB number.
Example:
11
44166181111
Your Output:
4(due to recursive equation, where you are adding 1)
Expected Output:
41
61
11
11

Solution:

  1. No need of recursion.
  2. use an array to mark the indexes that are used in the CB numbers that are already detected.

Approach:

  1. 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.

  2. Print the count

Consult the following program:

Hope, this would help.
If you still have any issue, feel free to ask.
Give a like if you are satisfied.

In the test case that you have provided, output should be 4 as we have to find maximum number of CB numbers not the CB numbers itself. So, in this case my code is giving correct output i.e. 4.

Hello @vaishnavtannu,

Yes, you are correct.
I misinterpreted the question earlier.

Now, coming back to your code:
It is failing for the test cases like:
Example 1:
5
23572
Your Output:
3
Expected Output:
5
CB Numbers:
2 3 5 7 2

Example 2:
9
235723371
Your Output:
5
Expected Output:
8
CB Number:
2 3 5 7 2 3 3 7

Example 3:
8
55362134
Your Output:
4
Expected Output:
5
CB Number:
5 5 3 2 3

Example 4:
14
23574414416111
Your Output:
6
Expected Output:
8
CB Number:
2 3 5 7 41 41 61 11

Let me know if you face difficulty in correcting your code.