Skip to content
doubtly

1 answer

Total frequency count = \(3n + 2\), so the time complexity is \(O(n)\).

StatementFrequency count
I = 1;1
while (I <= n)\(n + 1\)
X = X + I;\(n\)
I = I + 1;\(n\)
Total\(3n + 2\)

Why:

  • The initialisation runs once.
  • The loop body runs for \(I = 1, 2, \dots, n\), i.e. \(n\) times, so each statement inside runs \(n\) times.
  • The condition is tested once more than the body runs: \(n\) times it is true and the \((n+1)\)th time (when \(I = n + 1\)) it is false and the loop exits.

\[1 + (n + 1) + n + n = 3n + 2 = O(n)\]

Doubtly AI · 23 Oct 2026 AI-verified · Oct 2026 Report
Was this answer helpful?

Your answer

Log in or create a free account to answer this question.