✏️ Explanatory Question

Calculate the run-time efficiency of the following program segment:

i = 1
loop (i <= n)
    print(i)
    i = i + 1
end loop

👁 0 Views
📘 Detailed Answer
🟢 Easy
💡

Answer with Explanation

Answer: The variable i iterates n times. Therefore, the run-time efficiency is O(n).

💡 Explanation: The loop starts with i = 1 and increases i by 1 after every iteration. It continues until i > n. Therefore, the loop body executes exactly n times.

Each iteration performs constant-time operations: one print operation and one increment operation. Therefore:

Total work = n × constant = O(n)

Key Idea: A single loop that processes n items is a standard example of linear time complexity.