Problem cb numbers

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt(),count = 0;
String str = cin.next();
for(int i = 0;i<str.length();i++){
for(int j = i+1;j<=str.length();j++){
int x = Integer.parseInt(str.substring(i,j));
if(isValid(x)){
count++;
}
}
}
System.out.print(count);

}
public static boolean isValid(int y){
	int arr[] = {2,3,5,7,11,13,17,19,23};
	if(y==0||y==1){
			return false;
		}
	for(int i  = 0;i<arr.length;i++){
		if(y == arr[i]){
			return true;
		}
	}
	for(int i  = 0;i<arr.length;i++){
		 if(y%arr[i] == 0){
			return false;
		}
	}
	return true;

}

} =====I have found that for eg in sample input 5 81615 , I m also counting 615 along with 61 and 5 , please tell how to remove that by editing my code

also i deleted 29 by mistake but thats’s not the problem with my input

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.

or u can store the cb no starting index and ending index in the treeset
suppose ur starting indes=si
and ending index is=ei
then in the tree set floor of si should be less than si
and ceil of ei in the tree set shuould be larger than ei

if any of above condintion is not satisfied mean ur sub arr is overlapping with other

hope this will help u
pls rate my work

can u please show me implementation of your first approach in my code

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.