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
As we all know initially the elements in the array is 0. therefore b is the ans.. :)
array is from $A\left [ 0,....(n-1) \right ]$
Then how r u taking top1=-1 and top2=n??
But , see the question again
It is asking "number of elements are present in the array at any time"
is that mean anytime stack contains $0$ element?
Initially stack contains 0 element and i have used my test case for the same.. if i insert 2 elements now one from top1 and another from top2 the value of top1 becomes 0 and value of top2 becomes n-1
now putting these values in b) n+1−top2+top1 we get n+1-(n-1)+1 = 2 i.e. there are 2 elements in the array..
so it is clear that formula b is the right answer for ANY time..
yes, My bad
Got the logic.
In my example
There is top1 is in a, and top1 is in a
So, gap between in this array is 2 elements , i.e. a and a.
But when we actually subtracting , we got 5-2=3 element
To cancel out that extra 1 we have to subtract it from (n+1) elements
And thus got total elements
@sagar2405 explain plz