Question
The given diagram shows the flowchart for a recursive function . Assume that all statements, except for the recursive calls, have time complexity. If the worst case time complexity of this function is , then the least possible value (accurate up to two decimal positions) of is ________.
Official answer and solution
- Accepted answers
- 2.2 to 2.4
- From
- Official GATE 2016 answer key
Solution
Trace all execution paths in the flowchart to find the worst-case number of recursive calls, then apply the Master Theorem.
Trace all possible paths from Start to Return in the flowchart:
- Path 1: Start Diamond 1 Left Return.
Total recursive calls: calls.
- Path 2: Start Diamond 1 Right Diamond 2 Right Return.
Total recursive calls: calls.
- Path 3: Start Diamond 1 Right Diamond 2 Down Diamond 3 Right Return.
Total recursive calls: calls.
- Path 4: Start Diamond 1 Right Diamond 2 Down Diamond 3 Left Return.
Total recursive calls: calls.
In the worst case, the function executes Path 4, making 5 recursive calls of input size . All other statements take time, yielding the recurrence:
By the Master Theorem (Case 1) with :
Rounding to two decimal positions gives .
2.32
Missing the initial before Diamond 1 or miscounting the sequential calls along the right branch of Diamond 1.
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2016 Computer Science and Information Technology (CS) Session 2
- Question
- 49 of 65, printed as CS Q.39
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.