2,208 views
1 votes
1 votes
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 votes
1 votes

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 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

Related questions

2 votes
2 votes
2 answers
4
jatin khachane 1 asked Dec 1, 2018
1,275 views
My doubt : What should we consider ^ operator as Bitwise XOR ? or Exponentiation