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;