how to approach this problem ??? im not getting it,
and also tell me if i will require sieve of eratosthenes…!!!
Playing with Cards
Hey @akb.tech17
It’s a simple brute force approach question. Just keep all numbers in a queue initially. Then keep on putting numbers on B or A according condition till Q iterations. No you don’t need sieve for this.
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.
hello @akb.tech17 yes you have to prepare prime sieve of 1e5 and then you can perform the operation .
INPUT :
N=5
Q=2
A(0)th stack : 1 2 3 4 5
Processing :
iterate for i =1 to Q;
for i=1(frst iteration):
- Pop top element of A(i-1)th (A(0)th) stack i.e. 5
- Check, ((top_element i.e. 5)%(ith prime number i.e. 1st prime number, 2)) == 0 ? Push top_element to B(i) i.e. B(1)th stack : Push top_element to A(i)i.e.A(1)th stack;
After repeating the same for all elements of stack A(0):
A(0)=[] i.e. empty as all elements have been popped.
A(1)=[5,3,1] as they are not divisible by 2.
B(1)=[4,2] as they are divisible by 2.
for i=2=Q (second and last iteration):
- Pop top element of A(i-1)th (A(1)th) stack i.e. 1
- Check, ((top_element i.e. 1)%(ith prime number i.e. 2nd prime number, 3)) == 0 ? Push top_element to B(i) i.e. B(2)th stack : Push top_element to A(i)i.e.A(2)th stack;
After repeating the same for all elements of stack A(1):
A(1)=[] i.e. empty as all elements have been popped.
A(2)=[1,5] as they are not divisible by 3.
B(2)=[3] as they are divisible by 3.
Output:
print elements of B(1),B(2),A(2)
2
4
3
5
1
this is the code for this question try to go through the question and let me know if you have nay doubt .
Happy Learning !!
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.