‹ Back to the paper
The worst-case complexity for following code segment is: for (int i = 0; i < p; i++) { a +=…
The worst-case complexity for following code segment is:
for (int i = 0; i < p; i++)
{
a += Math.sqrt(i);
}
for (int j = 0; j < q; j++)
{
b += Math.log(j + 1);
}- (a)$O(1)$
- (b)$O(p \times q)$
- (c)$O(p^2 + q^2)$
- (d)$O(p + q)$
Answer
Answer
AICorrect option: (d)
Answer: (d) $O(p + q)$
The two for-loops are independent (sequential, not nested): the first runs $p$ times giving $O(p)$, the second runs $q$ times giving $O(q)$. Adding two independent loop complexities gives worst-case complexity $O(p) + O(q) = O(p+q)$.
From ISC 2026 Improvement Computer Science Paper 1, question 1(v).