3 testcases getting failed
Cb number string
Hello @rockstarpkm,
-
The logic you have applied for is prime will fail for:
23,29
for i=23, it will also check for divisibility by i+2=25 which will be wrong.
Moreover, the logic will check the divisibility by prime numbers greater than 29 like 31 which is not required by this question.
Better way:
Just define an array initialized with all the following elements:
2,3,5,7,11,13,17,19,23,29
Then simply write a logic for point 1, 2 and 3 given in the question using this array. -
As you have to find the maximum number of CB numbers possible.
So, start checking for all the sub-strings of size 1 and then increment the size one by one. -
As the indexed used in one CB number cannot be used in the other as explained in the example in the question.
So, created an array called visited to mark those indices that are the part of CB numbers that have been detected so far.
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.
Give a like if you are satisfied.