Question
Consider functions Function 1 and Function 2 expressed in pseudocode as follows:
Function 1 Function 2
while n > 1 do for i = 1 to 100 * n do
for i = 1 to n do x = x + 1;
x = x + 1; end for
end for
n = floor(n/2);
end while
Let and denote the number of times the statement "x = x + 1" is executed in Function 1 and Function 2, respectively.
Which of the following statements is/are TRUE?
Official answer and solution
- Official answer
- A and D
- From
- Official GATE 2023 answer key
Solution
Sum loop iterations for both functions and compare their asymptotic growth rates.
-
Function 1:
In each iteration of thewhileloop, the inner loop executes times, and then is halved (). The total executions ofx = x + 1is:This is a geometric series bounded by . Therefore, .
-
Function 2:
The loop executes for to . Hence: -
Since both functions grow linearly with ( and ):
- is TRUE (Statement A).
- is TRUE (Statement D).
- Statements B () and C () are strictly false since the ratio limits to a non-zero constant ().
Options (A, D)
Assuming the while loop halving introduces a logarithmic term , forgetting that the work halves in each step ().
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2023 Computer Science and Information Technology (CS)
- Question
- 54 of 65
- 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.