Can you please guide me that what i should do to optimize my code .
I have used prime Sieve method but still TLE
@1999atrijsharma
According to the Problem the work is to find the Max CB numbers from the given number.
As to work on substrings of the given number, we will capture the element into the String instead of integer .
The number ‘n’ given is nothing to do with our approach(as we are working on String) but can be useful according to your approach.
Intuition to solve this problem:
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.
Reference Code
i hope this helps
@1999atrijsharma
No you don’t need to check for prime numbers buddy. You have to check for divisibility with the given numbers, that’s it.
maam can you please tell me as per question both 8161 and 61 are CB number then how to differntiate between them as 8161 come before 61
In the question you have to find the maximum number of CB numbers that you can form from the input:
In the Sample Testcases:
Example 1:
5
81615
There are at max two CB numbers that we can form:
61
5
Thus, the output is 2.
Approach:
- 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. - Print the count
try to use this approach and refer to the code provided for reference
Hope, this would help.
applied the condition in https://ide.codingblocks.com/s/323282 still getting test case not passed can you please check my code again
can you please reply maam , how to correct my code ?
Submission Not Judged View 0/100 – java Passed: maam what does it mean??
Maam/sir still all test cases not passed and now run time error is arising , can you please refer to my recent submission ??
have you identify my mistake ??
@1999atrijsharma,
https://ide.codingblocks.com/s/326839 corrected code.
Errors:
Use long in the Cb method. Also, initially the boolean array contains all values as false. Hence, Arrays.fill(visited,false); is redundant.