Prateek loves candy please tell me the error

public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int t = s.nextInt();
int a[]=new int[t];
for (int i = 0; i < t; i++) {
a[i] = s.nextInt();

	}
	
	boolean p[] = new boolean[1000];
	for (int i = 0; i < p.length; i++) {
		p[i] = true;

	}
	
	ArrayList<Integer> list = new ArrayList<Integer>();
	for (int i = 2; i * i < 1000; i++) {

		if (p[i] == true) {
			for (int j = i * i; j < 1000; j += i) {

				p[j] = false;
				
				
			}

		}
		
	}
	for(int k =2;k<=1000;k++){
		if(p[k]==true) {
			list.add(k);
		}
	}
	for(int i=0;i<t;i++)
	{
		System.out.println(list.get(i));
	}
	
}

your logic was correct but there were some minor errors refer to this commented code…

import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int t = s.nextInt();
int a[]=new int[t];
for (int i = 0; i < t; i++) {
a[i] = s.nextInt();

	}
	//it is mentioned in the ques that ans can be of the order of 10^6 so we have to create an array of 10^6 only
	boolean p[] = new boolean[1000000];
	for (int i = 0; i < p.length; i++) {
		p[i] = true;

	}
	
	ArrayList<Integer> list = new ArrayList<Integer>();
	for (int i = 2; i * i < 1000000; i++) {

		if (p[i] == true) {
			for (int j = i * i; j < 1000000; j += i) {

				p[j] = false;
				
				
			}

		}
		
	}
	//condition of k was not correct in your code 
	for(int k =2;k<1000000;k++){
		if(p[k]==true) {
			list.add(k);
		}
	}
	for(int i=0;i<t;i++)
	{
		System.out.println(list.get(a[i]-1));//here it shoouldnt be i
	}
	
}
}

what is error please tell

as the array is of size 10^6 in the for loops the correct condition should be i<10^6 and not i<=10^6

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.