2 test casses are not passing
Hey @Amogh_Agarwal Your code is failing test case like :
8
86922395
Correct ans = 4
Your output = 2.
what should I change in my code to resolve it? If I am changing it then the code is not passing more test cases
@Amogh_Agarwal Benchmark your code with my code below :
import java.util.*;
public class cbNos {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
scn.nextInt();
String str = scn.next();
int count = 0;
boolean[] visited = new boolean[str.length()];
for (int len = 1; len <= str.length(); len++) {
for (int si = 0; si <= str.length() - len; si++) {
int ei = si + len;
String ss = str.substring(si, ei);
if (isCBNo(Long.valueOf(ss)) && isValid(visited, si, ei)) {
count++;
for (int i = si; i < ei; i++) {
visited[i] = true;
}
}
}
}
System.out.println(count);
}
public static boolean isValid(boolean[] visited, int start, int end) {
for (int i = start; i < end; i++) {
if (visited[i]) {
return false;
}
}
return true;
}
public static boolean isCBNo(long n) {
if (n == 0 || n == 1) {
return false;
}
long[] arr = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 };
for (int i = 0; i < arr.length; i++) {
if (n == arr[i]) {
return true;
}
}
for (int i = 0; i < arr.length; i++) {
if (n % arr[i] == 0) {
return false;
}
}
return true;
}
}
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.