In the first example, it turned out: O (n), I don’t know why.
Example 1:
for (k = 0; k <= n / 8; k++)
System.out.println(k);
System.out.println("Hello World!");
for (p = n; p >= 1; p--)
System.out.print(p % 2);
For the second example. it turned out to be O (n ^ 2), not knowing why.
Example 2:
for (k = 0; k < n - 1; k++)
for (m = k + 1; m < n; m++)
System.out.println(k * m);
In the third example, this turned out to be O (n), but not sure why.
Example 3:
for (i = n - 3; i <= n - 1; i++) {
System.out.println(i);
for (k = 1; k <= n; k++)
System.out.println(i + k);
}
The last example turned out to be O (n ^ 2), also not sure why.
Example4:
for (a = 1; a <= n / 3; a++)
for (b = 1; b <= 2 * n; b++)
System.out.println(a * b);
Can someone please read them and explain what I'm doing wrong. My reasoning is that we are tracking the variable "n", because this is what the user entered, and we see how it grows. If it is one operator, then the constant time is O (1), if it is in a loop, which is O (n) by default, if it is in a nested loop, that is O (n ^ 2).
Please help thank you