Finding the CB numbers

please explain the approach

Hey @Shubh-Garg-2556190207986238
in this problem you have to find the number of CB numbers in the string. CB numbers are defined under the following criteria

  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. They use following criteria to define a CB Number…

so what you can do is

  • Put loop on string that will give substring of every length.
  • create a function that will return true if the passed number is a CB number otherwise return false.
  • To put a check if the digit is already a part of the any other CB number, create an boolean array say, valid which store which digits till now has been a part of any other CB number.
  • Take a counter and increment if a CB number is found.
  • At the end print the count.

code :- https://ide.codingblocks.com/s/352807

Imp point to note is : CB number once detected should not be sub-string or super-string of any other CB number.so To do this question you must take all the substrings of length 1 followed by all the substrings of length 2 and so on and check if they are CB numbers if they are increment the counter and make the character unavailable for next substrings.

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.