why the answer is coming incorrect, i checked the solution with editorial…
import java.util.*;
public class Main
{
public static void main(String[] args)
{
sieve();
Stack s1= new Stack<>();
Stack s2= new Stack<>();
Stack s3= new Stack<>();
Scanner sc= new Scanner(System.in);
int n=sc.nextInt();
int q=sc.nextInt();
for(int i=0;i<n;i++)
{
s1.push(sc.nextInt());
}
int q=sc.nextInt();
//int d=prime.get(q-1);
Isdivisible(s1,s2,s3,q);
}
static final int max=1299709;
static boolean[] sieve= new boolean[max];
static ArrayList prime= new ArrayList<>();
static void sieve()
{
for(int i=0;i<max;i++)
{
sieve[i]=true;
}
sieve[0]=false;
sieve[1]=false;
for(int i=2;i<max;i++)
{
if(sieve[i]==true)
{
prime.add(i);
for(int j=i*i;j<max && j>=0;j=j+i)
{
sieve[j]=false;
}
}
}
}
public static void Isdivisible(Stack<Integer> s1,Stack<Integer> s2,Stack<Integer> s3,int q)
{
for(int i=0;i<q;i++)
{
while(!s1.isEmpty())
{
int item=s1.pop();
int d=prime.get(i);
if(item%d==0)
{
s2.push(item);
}
else
{
s3.push(item);
}
}
}
while(!s2.isEmpty())
{
s1.push(s2.pop());
}
while(!s3.isEmpty())
{
s1.push(s3.pop());
}
Iterator itr= s1.iterator();
while(itr.hasNext())
{
System.out.print(itr.next()+" ");
}
}
}