Runtime of for loop-Q2
Problem: Find the time complexity for a nested for loop.
1
2
3
4
5
6
7
8
9
10
A() {
int i, j, k ,n;
for(i = 1; i <= n; i++) {
for(j = 1; j <= i; j++) {
for(k = 1; k < 100; k++) {
System.out.println("hello world");
}
}
}
}
For each iteration of the inner loop executes a 100 times.
$$ i = 1 \\ 1 \times 100 $$ | $$ i = 2 \\ 2 \times 100 $$ | $$ i = 3 \\ 3 \times 100 $$ | $$ i = 4 \\ 4 \times 100 $$ | $$ i = 5 \\ 5 \times 100 $$ |
Answer: