Finding cb number issue

Hi.
3 of the test case have been failed (i.e, 1,2,3). Can you please check and let me know what’s wrong with this code?

Scanner sc = new Scanner(System.in);
int len = sc.nextInt();
String str = sc.next();
int ans = 0;
int ansArr[] = new int[len];
int arr[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 };
for (int i = 0; i < len; i++) {
int start = i;
int end = i + 1;
while (end <= len) {
int condition = Integer.parseInt(str.substring(start, end));
if (condition > 1) {
if(checkIfDivides(arr,condition)) {
if(checkIfUsed(ansArr, start, end)) {
ans++;
}
start = end;
end++;
} else {
end++;
}
} else {
start++;
end++;
}
}
}
System.out.println(ans);
}
public static boolean checkIfDivides (int arr[], int val) {
for (int i = 0; i < arr.length; i++) {
if(arr[i] != val) {
if(val % arr[i] == 0) {
i = arr.length - 1;
return false;
}
}
}
return true;
}

public static boolean checkIfUsed (int arr[], int start, int end) {
	for (int i = start; i < end; i++) {
		if(arr[i] == 1) {
			i = end + 1;
			return false;
		} else {
			arr[i] = 1;
		}
	}
	return true;

Hey @patnamsainikhil Test case which is failing is :
11
44166181111
Correct o/p = 4
Your output : 1

Hey @patnamsainikhil You have to take no. of CB numbers that can be formed. You don’t have to print CB numbers itself and here 4 is the count not a CB numbers

Hi ,
The first number which I got is 44166181 and as other numbers cannot be a substring of this number, the answer is only one as per the code.
Can you please let me know which part of the code has a fault or can you help me with a different approach?

Hey @patnamsainikhil
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 acoording to your approach.

Intuition :

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

Thank you,
Also, can you help me with the substrings of 44166181111, even when I had calculated manually, I’m getting only 3 substrings other than 44166181(i.e, 41, 661, 811)? Can you please help me with the 4th number?

Hey @patnamsainikhil The CB numbers in
11
44166181111
Are :
41
61
11
11
Here 1 is two times bcoz we have four ‘1’.

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.