The problem is looking easy but it's not. The way we are going to tackle it is recursion. The problem is simple when you see it from recursion perspective.
Key: The number of steps required to shift the stack is exactly equal to the twice of steps for shifting the stack of one less disk(The largest one) plus one step.
Consider the case of shifting one disk : T(1) = 1
Consider the case of shifting two disk : T(2) = 2*T(1) + 1 = 3
Consider the case of shifting three disk : T(3) = 2*T(2) + 1 = 7
.
.
.
.
T(n) = 2*T(n-1) + 1
So here is the code:
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer