Prime Visits TLE

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; p
p < 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);
	}
}

}

@satwik7142,
https://ide.codingblocks.com/s/238218 corrected code.
Don’t store prime numbers in arraylist, instead just iterate through the prime array to check for prime numbers between a and b.

abc.contains() iterates over the entire arraylist for every c. Hence it increases the time complexity massively. Keep it simple.

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.