My code is showing TLE in 1 of 3 cases. I cannot think of any other way to further optimize it.
Here is my code-
import java.util.;
public class Main {
public static void main(String args[]) {
boolean[] prime = new boolean[1000005];
for(int i=3; i<1000001; i+=2){
prime[i] = true;
}
prime[2] = true;
for(int p = 2; pp < 1000005; p++){
if(prime[p]==true){
for(int x = p*p ;x<1000005;x +=p ){
prime[x] = false;
}
}
}
ArrayList<Integer> abc = new ArrayList<>();
abc.add(2);
for(int j = 3; j<1000001; j +=2){
if(prime[j]==true){
abc.add(j);
}
}
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int counter;
for(int k=0; k<n; k++){
counter = 0;
int a = scn.nextInt();
int b = scn.nextInt();
for(int c=a; c<b+1;c++){
if(abc.contains(c)==true){
counter++;
}
}
System.out.println(counter);
}
}
}