Prateek Loves Candy

Using System.currentTimeMillis(), i’ve got my time to be around 20ms still I am getting timelimit error, pls help!

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int r=1,i=2;
int m=0;
int n1=scan.nextInt();
while(r<=n1){
int n = scan.nextInt();
int count=n;

int arr[] = new int[1000001];
arr[0]=0;
arr[1]=0;
for(int k =2;k<1000001;k++){
    arr[k]=1;}

while(i<=1000000){
     m=0;
    if(arr[i]==1){
        for(int j=2;j*i<=1000000;j++){
            if(arr[i*j]!=0)
            arr[i*j]=0;
        }
    }
    if(arr[i]==1){

m=i;
count–;
}
i++;
if(count==0)
break;
}

    System.out.println(m);
    
    i=2;

r++;
}

}
}

Hey @akashbisht7,
I have modified your code a little. Your approach was correct, just some slight errors. https://ide.codingblocks.com/s/151137
But there is one more mistake in this. So please find that and correct it. This code will give you answer only if, the answer is less than 1000.
Kindly find the mistake and try yourself. If you are stuck somewhere, feel free to ping me.

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.