1 votes 1 votes If two stack is growing two opposite end of array. Then which logic works and how? DS stack data-structures + – srestha asked Dec 24, 2018 srestha 1.5k views answer comment Share Follow See 1 comment See all 1 1 comment reply neeraj33negi commented Dec 24, 2018 reply Follow Share Can you please explain the question? 0 votes 0 votes Please log in or register to add a comment.
Best answer 2 votes 2 votes As much i understand from question two stack implementation using single array. Array[1........n] Stack1 with top1=0 [when element added in stack1 top1++] push increment and pop decrement in top1. stack2 with top2=n+1[when element added in stack2 top2--]push decrement and pop increment in top2(may be bug as A[n+1] so we set top2 acc] if array full top1 +1 =top2 if array empty top1=n-top2+1 Abhisek Tiwari 4 answered Dec 24, 2018 • edited Mar 14, 2019 by Abhisek Tiwari 4 Abhisek Tiwari 4 comment Share Follow See all 9 Comments See all 9 9 Comments reply Show 6 previous comments srestha commented Dec 24, 2018 reply Follow Share I havenot got plz elaborate more any link? 0 votes 0 votes Abhisek Tiwari 4 commented Dec 24, 2018 i edited by Abhisek Tiwari 4 Dec 24, 2018 reply Follow Share A[1......10] here top1=0 top2=-1 >at starting 1.stack top1=0 push 5 top1++ i.e 1 push 7 top1++ i.e 1+1=2 push 1 the top1 become3 2.stack2 top2 =-1(assume) (First push(3) top2=n=10 as top2==-1 then next pushes(2) top2-- i.e 9 ] First push in stack2 i.e when top2=-1 then top2 will be set to n,further push will just decrement top2. for more see push and pop code. https://www.geeksforgeeks.org/implement-two-stacks-in-an-array/ 0 votes 0 votes aimhigh commented Dec 24, 2018 reply Follow Share https://gateoverflow.in/1000/gate2004-3 question is well framed. & It's mentioned that space is used efficiently & stack grows from topmost then top2=top1+1 0 votes 0 votes Please log in or register to add a comment.