3,343 views
1 1 vote
Consider a single array $A\left [ 0...........(n-1) \right ]$ is used to implement two stacks. Two stacks grows from opposite end of the array. Variable  $top_{1}$ and $top_{2}$ points to the location of the topmost elements in each of the stacks with initial values of $-1$ and $n$ respectively and $top_{1}<top_{2}$ always. If  certain push and pop operations are performed in either end, then which of the following represents the number of elements are present in the array at any time?

$A)n-top_{2}+top_{1}$

$B)n+1-top_{2}+top_{1}$

2 Answers

Best answer
1 1 vote

Ans is B

Initially the array must be empty that is, it must contain 0 number of elements. Now we are given that top 1 = -1 and top2= n

when no elements are inserted the 2 pointers remain in their same location, i.e. -1 and n

now putting these values in equation a and b  we get

a) n-top2+top1

= n-n+(-1)

= -1

b) n+1−top2+top1

 = n+1-n-1

=0

As we all know initially the elements in the array is 0. therefore b is the ans.. :)

• selected by
Position:
Show:

No related questions found