Open In App

GATE | Gate IT 2005 | Question 13

Like Article
Like
Save
Share
Report
A function f defined on stacks of integers satisfies the following properties. f(∅) = 0 and f (push (S, i)) = max (f(S), 0) + i for all stacks S and integers i.

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.


Quiz of this Question


Last Updated : 28 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads