for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j += i) {
// some code
}
}
The outer loop definitely runs n times. In case of the inner loop, assuming n = 8,
| i | j |
|---|---|
| 1 | 1, 2, 3, 4, 5, 6, 7, 8 ---> ran N times |
| 2 | 1, 3, 5, 7 ---> ran N/2 times |
| 3 | 1, 4, 7 |
| 4 | 1, 5 |
| 5 | 1, 6 |
| 6 | 1, 7 |
| 7 | 1, 8 |
| 8 | 1 |
I am confused whether the complexity should be logn or n for the inner loop. Any help will be greatly appreciated!