PRASHNIKAप्रश्निका
‹ Back to the paper

Answer the following question.

How would the complexity change if all the three loops went to instead of , and ?

Computer Science20171 markShort answer
How would the complexity change if all the three loops went to $N$ instead of $a$, $b$ and $c$?
Show the code
for (int x = 1; x <= a; x++)
{
    statements;
}
for (int y = 1; y <= b; y++)
{
    for (int z = 1; z <= c; z++)
    {
        statements;
    }
}

Answer

Answer

AI
$O(N^2)$ With all loops running to N the complexity becomes $O(N + N \times N) = O(N + N^2)$, and the dominant term $N^2$ gives $O(N^2)$.
Complexity and Big O notation

From ISC 2017 Computer Science Paper 1, question 2(d)(ii).