GATE | Gate IT 2005 | Question 13
If a stack S contains the integers 2, -3, 2, -1, 2 in order from bottom to top, what is f(S)?
(A) 6
(B) 4
(C) 3
(D) 2
Answer: (C)
Explanation:
f(S) = 0, max(f(S), 0) = 0, i = 2
f(S)new = max(f(S), 0) + i = 0 + 2 = 2
f(S) = 2, max(f(S), 0) = 2, i = -3
f(S)new = max(f(S), 0) + i = 2 – 3 = -1
f(S) = -1, max(f(S), 0) = 0, i = 2
f(S)new = max(f(S), 0) + i = 0 + 2 = 2
f(S) = 2, max(f(S), 0) = 2, i = -1
f(S)new = max(f(S), 0) + i = 2 – 1 = 1
f(S) = 1, max(f(S), 0) = 1, i = 2
f(S)new = max(f(S), 0) + i = 1 + 2 = 3
Thus, option (C) is correct.
Please comment below if you find anything wrong in the above post.
Attention reader! Don’t stop learning now. Practice GATE exam well before the actual exam with the subject-wise and overall quizzes available in GATE Test Series Course.
Learn all GATE CS concepts with Free Live Classes on our youtube channel.



