The worst case complexity for following code segment is: ```java for(int i=1; i<= n; i++) { for(int…
The worst case complexity for following code segment is:
for(int i=1; i<= n; i++)
{
for(int j=1; j<= i; j++)
{
statement;
}
}- (a)O(n+i)
- (b)O(n×i)
- (c)O(n)
- (d)O(n²)
Answer
Answer
AIWritten by AI (antigravity) - it can contain mistakes.
Correct option: (d)
Answer: (d) O(n²)
The outer loop executes n times and for each i, the inner loop executes i times. Total executions = 1 + 2 + ... + n = n(n + 1)/2, which gives worst case complexity O(n²).
From ISC 2026 Computer Science Paper 1, question 1(v).