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

Answer the following question.

What is the worst case complexity of the code segment?

Computer Science20171 markShort answer
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(a + b \times c)$ The first loop runs a times and the nested loops run b x c times; the two segments are sequential, so their costs add.
Complexity and Big O notation

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