I am solving TDKPRIME - Finding the Kth Prime
I am using Sieve of Eratosthenes to answer the K queries. The length of the sieve array is 86028122 which is just enough to store the 5000000th prime number which is 86028121.
I am getting TLE error. Below is my code. Please suggest to me how can I optimize it further.
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
int SIEVE_LEN = 86028122;
int PRIMES_LEN = 5000000;
boolean[] sieve = new boolean[SIEVE_LEN];
int[] primes = new int[PRIMES_LEN];
int count = 0;
sieve[0] = true; sieve[1] = true;
// if prime -> sieve[i] = false, else -> sieve[i] = true
for(int i = 2; i < SIEVE_LEN; i++) {
if(sieve[i] == false) {
primes[count] = i;
count++;
for(int j = 2*i; j < SIEVE_LEN; j+=i) {
sieve[j] = true;
}
}
}
while(t-- > 0) {
int n = sc.nextInt();
System.out.println(primes[n-1]);
}
}
}