I have used Sieve of Eratosthenes technique , but still i am getting Time Limit Exceeded problem . Can you tell me how to get rid of this problem .
This is my code :
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in) ;
int n = scan.nextInt() ;
while(n-- > 0){
int p = scan.nextInt() ;
int ans = 0 ; int count = 0 ;
boolean[] arr = new boolean[1000000] ;
ArrayList<Integer> al = new ArrayList<Integer>() ;
for(int i = 0 ; i < arr.length ; i++ )
arr[i] = true ;
arr[0] = false ; arr[1] = false ;
for(int i = 2 ; i <= Math.sqrt(1000000) ; i ++){
if(arr[i] == true){
for(int j = i*i ; j < arr.length ; j+= i){
arr[j] = false ;
}
}
}
for(int i = 2 ; i < arr.length ; i ++){
if(arr[i] == true){
al.add(i) ;
}
}
System.out.println(al.get(p-1)) ;
}
}
}