i made the code which checks count of cb numbers of equal length i.e in 81615 it will check only 81,16,61 and 15 when we take length 2. it will not consider one number of length 3 and other of 2. can you plz tell how to do it.
Doubt in concept
Hello @Akshita99
What you can do is consider that your current cb number ends at index βiβ and the previous cb number ended at index βkβ (k < i)
(here βiβ start from 0 and ends at n - 1)
then what you can do is start from index βiβ and go backwards to k + 1 and check if
number(i, i) is a cb or not
number(i-1, i) is a cb or not
number(1-2, i) is a cb or not
β¦
number(k + 2, i) is a cb or not
number(k + 1, i) is a cb or not
if(a cb number is found) update k
Hello @Akshita99
Here is my code
Read the comments in the code to know what each function does.
My code on test case
5
81615
First
8 is checked β NOT a cb number
then
1 is checked β NOT a cb number
81 is checked β NOT a cb number
then
6 is checked β NOT a cb number
16 is checked β NOT a cb number
816 is checked β NOT a cb number
then
1 is checked β NOT a cb number
61 is checked β a cb number // FOUND A CB NUMBER cnt++; break;
then
5 is checked β a cb number // FOUND A CB NUMBER cnt++; break;
then
NO more string left.
If you still need any help, leave a reply to this thread and I will help you out.
is it possible in some case we still do net get right answer? eg suppose in 81225 we consider that 812 is a cb number(suppose) and 2 and 5 are also cb numbers so we get 3 cb numbers.But our code stops at 81 which is also a cb number(suppose) and then only 5 is a cb number. so we will get output of only 2 cb numbers instead of 3? i have not checked whether they are actually cb numbers or not, i just took any random number
Hello @Akshita99
We will get the correct answer every time because our code searches for the smallest length from the start that contains a CB number.
Ex:-
We have the first index as 0 and the last index as n - 1 in the string.
Let us have two indices as i and j where 0 < i < j < n - 1
Here letβs suppose that from 0-i is the smallest length which contains a CB number (smallest length would imply that the CB number will end at index βiβ)
Letβs suppose that from length 0-j there are
- only one CB number
Then in this case we will choose only the 0-i CB number because then we will have larger remaining string to search for more CB numbers. - Two or more CB numbers
Ex:-
812566789
Let CB numbers be 81 812 566 (Here j = 5)
Then also we will choose the smallest length CB number 81 (and NOT 812) because that will also leave us with a larger string to search for more CB numbers.
The crux is if we can get a CB number in smaller length string then why would we choose a string of larger length to have only one CB number
If you still need any help, leave a reply to this thread and I will help you out.