Let
be a stack of size greater than
. Starting from an empty stack, suppose we push the first
natural numbers in sequence and then perform
pop operations. For
the stack life of
is defined as the time taken from the end of
push(x)
to the start of the pop operation that removes
from
. What of the following is the correct representation for the average stack life of
?
Details and Assumptions
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Stack Life Drawing The stack life of x = 1 is equal to 2 ( n − 1 ) ( k + l ) + l = ( 2 n − 1 ) ( k + l ) − k (see the drawing).
Likewise, the stack life of i ∈ [ 1 , n ] ∩ N is equal to 2 ( n − i ) ( k + l ) + l = ( 2 n − 2 i + 1 ) ( k + l ) − k
Therefore, the average stack life is :
n 1 i = 1 ∑ n ( ( 2 n + 1 ) ( k + l ) − k − 2 i ( k + l ) ) = ( 2 n + 1 ) ( k + l ) − k − n 1 2 2 ( k + l ) n ( n + 1 ) = n ( k + l ) − k