Playing cards in stack

public static void main(String[] args) throws Exception {
	Scanner scn = new Scanner(System.in);
	int n = scn.nextInt();
	playcardsstack obj = new playcardsstack();
	stackusingarray s1 = obj.new stackusingarray(n);
	stackusingarray A1 = obj.new stackusingarray(n);
	stackusingarray B = obj.new stackusingarray(n);

	int q = scn.nextInt();
	int[] A = new int[n];
	for (int i = 0; i < n; i++) {
		A[i] = scn.nextInt();
	}
	play(A, q, s1, A1, B, n);
}

public static void play(int[] A, int q, stackusingarray s1, stackusingarray A1, stackusingarray B, int n)
		throws Exception {
	for (int i = 0; i < A.length; i++) {
		s1.push(A[i]);
	}
	int tp;
	int j = 1;
	while (j <= q) {
		for (int k = 1; k <=n; k++) {
			while (!s1.isempty()) {
				tp = s1.pop();
				if (tp % 2 == 0) {
					B.push(tp);
				} else {
					A1.push(tp);
				}

			}
		}
		j++;

	}
	B.display();
	A1.display();
}

test case for this question playing card in stack is failing …i think i am going wrong in division by ith prime case…plzz check

Hi Talha,in this question you have to check at every ith iteration if the number you have taken is divisible by the Ith prime number if yes then put it in stack 2 else put it in stack 3 but here you have only checked its mod with 2 hence its not giving the correct ans,here you can make a function in which you have stored all the prime numbers and then use that to check its mod and later you have to pop every element of stack2 as it will be used in other iterations also.
I am also sharing with you the code for your help
https://ide.codingblocks.com/s/59621
here i have used stack.peep which is the same as stack.top so you can make the changes accordingly,i have used the inbuilt stack functions in java.

i did not get this question can anyone plzzz explain it to me again …

Hi, in this question you have 3 stacks A0,A1,B1.A0 is the original stack which you have given as input.Then you will pick one by one each and every element from the top and check that whether the element you have picked in the ith iteration is divisible by the ith prime number or not,if it is divisible then put it on stack B1 else put it on stack A1.Then when stack A0 gets empty then print the stack B1 from bottom to top and stack A1 from top to bottom.

hii thanks for the help i got the logic well but could not code that …and the code u shared earlier seems complex and i am unable to understand pllzz explain the code too

Initially i have taken the input in stack1,then i have made a ArrayList primes which contains all the prime numbers now one by one i just take the top element of the stack in ele and in curr ith prime number,then i checked that if ele%curr==0 then push that ele in stack2 else in stack3 then i have printed all the elements of stack2 and then used stack3 as the new stack1 and again performed the same iterations q times,then in the last i have printed the stack1.

HII i have doubt in dynamic stack video…that when the size 5 is full we make another array of twice the size that is of size 10 than why we are able to add unlimited number of items in the stack even after making it of size 10…

Hi
as your top becomes equal to arr.length-1 then you make a new array of twice the size.It is not only when size=5 but for every time when top=arr.length-1.so you will be able to insert the unlimited number items in the stack.