in DS edited by
35,174 views
48 votes
48 votes

A single array $A[1 \ldots \text{MAXSIZE}]$ is used to implement two stacks. The two stacks grow from opposite ends of the array. Variables $top1$ and $top2$ $(top1 < top 2)$ point to the location of the topmost element in each of the stacks. If the space is to be used efficiently, the condition for $\text{“}\textsf{stack full}\text{”}$ is

  1. $(top1 = \text{MAXSIZE} / 2)$ and $(top2 = \text{MAXSIZE} / 2 + 1)$

  2. $top1 + top2 = \text{MAXSIZE}$

  3. $(top1 = \text{MAXSIZE} / 2)$ or $(top2 = \text{MAXSIZE})$

  4. $top1 = top2 - 1$

in DS edited by
35.2k views

4 Comments

This question was asked in BARC 2019

I couldn't add a tag to the question, probably because I don't have the question-editing privilege
3
3

Typo

(Top1 < Top2)

1
1
D is clear. :)

 

---------------------->    <----------------------------

                         T1   T2
0
0

This also known as colliding strategy.

CS13002 : Assignment 7 (iitkgp.ac.in)

0
0

6 Answers

62 votes
62 votes
Best answer

Answer is (D).

Since the stacks are growing from opposite ends, initially $top1 = 1$ and $top2 = MAXSIZE$. Now, to make the space usage most efficient we should allow one stack to use the maximum possible space as long as other stack doesn't need it. So, either of the stack can grow as long as there is space on the array and hence the condition must be $top1 = top2 - 1$.

edited by

4 Comments

@arjun SIR , 

The two stacks grow from opposite ends of the array. 

top 1 grows from begining and top 2 grows from end  the moment it crosses is top 1 <= N-top 2 then Option B should be correct ?

0
0

Gokhu top1 is 1st index and top 2 is last index . sum is maxsize is not for full stack condition na.

top1                                              top2

now seek top1+ top2= maxsize but stack contain 2 elements only.

12      13   14    15     16    17   18      19 (top1)     21(top2)   20  

here top1= top2-1 shows stack is full only.

11
11

Can you help me to clear below mentioned stack? Am not able to get " how top1 = top2 - 1 " and also what are these values 12,13,14,15,16,17,18,19,21 and 20 ?

1
1
@Prashant.Pethe top1= 7, top2=8(considering the array index starts from 0)

so top1=top2-1.

It's an array containing the elements 12,13,14,15,16,17,18,19,21 and 20 and top1 and top2 mark the top of the two stacks respectively where the 1st stack grows from left to right and the second one vice versa.
1
1
36 votes
36 votes

Consider array with 10 index:

top1                                              top2

Now seek Top1+ Top2= maxsize but stack contain 2 elements only. So Option B is fail.

12      13   14    15     16    17   18      19 (top1)     21(top2)   20  

here top1= top2-1 shows stack is full only. So Option D is correct.

                          top1                          top2

We see here stack is not full so condition Fail. So Option C is fail.

Option A is says both array have same size but there may be case that stack 2 contain 1 element and stack 2 contain 9 elements , which is not possible with case A.

D is answer

4 Comments

You haven't filled the entire array. So how can you say the stack is not full in case 3??

I may sound silly but someone please give a clarity.
0
0
In third case stack is empty  only from maxsize/2+1 to maxsize-1..so space is not use efficiently.
0
0
in option a top1 = maxsize /2 and top2 = maxsize/2 +1

if we use 1 in 2 then

top2 = top1 +1

=> top1=top2-1

and in this way the array is utilized efficiently so why is A a wrong answer
0
0
6 votes
6 votes

The idea is to start two stacks from two extreme corners of arr[]. stack1 starts from the leftmost element, the first element in stack1 is pushed at index 0. The stack2 starts from the rightmost corner, the first element in stack2 is pushed at index (n-1). Both stacks grow (or shrink) in opposite direction. To check for overflow, all we need to check is for space between top elements of both stacks

refer:

http://www.geeksforgeeks.org/implement-two-stacks-in-an-array/

6 votes
6 votes

Look at how heap and stack sections are incorporated in the Runtime Environment.

That is the most efficient way to utilize the space. If we divide the space by half, what if one entity requires 60% of the space, while the other entity requires none? Halving the available space is an inefficient way to utilize it.

Efficient utilization would be to let a stack take up all the space if the other stack doesn't want it.

 

For the array [1...MaxSize] Let top1 point to 0, and top2 point to MaxSize+1 in the beginning, since both the stacks are empty.

Stack full will be when:-

Option D

Answer:

Related questions