Playing with cards in stack

Submission #1207091
Failing 2 test cases why
#include <bits/stdc++.h>
#define MAX_SIZE 1000005
using namespace std;
void SieveOfEratosthenes(vector& primes)
{
// Create a boolean array “IsPrime[0…MAX_SIZE]” and
// initialize all entries it as true. A value in
// IsPrime[i] will finally be false if i is
// Not a IsPrime, else true.
bool IsPrime[MAX_SIZE];
memset(IsPrime, true, sizeof(IsPrime));

for (int p = 2; p * p < MAX_SIZE; p++) {
    // If IsPrime[p] is not changed, then it is a prime
    if (IsPrime[p] == true) {
        // Update all multiples of p greater than or
        // equal to the square of it
        // numbers which are multiple of p and are
        // less than p^2 are already been marked.
        for (int i = p * p; i < MAX_SIZE; i += p)
            IsPrime[i] = false;
    }
}

// Store all prime numbers
for (int p = 2; p < MAX_SIZE; p++)
    if (IsPrime[p])
        primes.push_back(p);

}

// bool find(int x){
// if(x==1){
// return false;
// }

// for(int i=1;i<x;i++){
// if(x%i==0){
// return -1;
// }
// }
// ret
// }
int main() {
int n,q;
cin>>n>>q;
int arr[n];
vector primes;

// Function call
SieveOfEratosthenes(primes);

stack<int>a;
stack<int>b;

for(int i=0;i<n;i++){
	cin>>arr[i];
}
for(int i=0;i<q;i++){
	int p=primes[i];
	
		for(int j=n-1;j>=0;j--){
		 if(arr[j]%p==0){
           a.push(arr[j]);
		 }
		 else{
			 b.push(arr[j]);
		 }
	 }
	
	
     
}
//cout<<endl;
while(!a.empty()){
	cout<<a.top()<<endl;
	a.pop();
}
while(!b.empty()){
	cout<<b.top()<<endl;
	b.pop();
}

return 0;

}

hi @kumarakash121005
A simple approach would be to simply make two arrays of stacks and solve as guided. We can precompute the primes using sieve to save time. This approach will take a lot of space as the sizes of arrays will be size = q+1.
Refer this code -->

what is wrong in my code

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.