GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

Find if possible to create second array from first array using a stack in between

Given an array A, all elements of it are copied to second array B using a stack in between.

  • The elements are picked up from array A one by one from end.
  • The elements are inserted into array B from beginning.
  • In the stack two operations are possible a). enque b) deque

Question: given two arrays tell if the second can be created using first array using the above constraints.


A =[1,2,3] , B = [2,1,3]

Operations :

  1. Eq : Stack =[3],B =[]
  2. Eq : Stack=[3,2], B=[]
  3. Dq : Stack=[3], B=[2]
  4. Eq: Stack=[3,1], B=[2]
  5. Dq: Stack=[3], B=[2,1]
  6. Dq: Stack=[], B=[2,1,3]

=> possible

A=[1,2,3] , B [1,3,2] : not possible

asked Aug 27, 2016 by zaks