Always consider what happens when the worst case occurs.
Constant-time: O(1)
Running time is constant and is not dependent on the size of input. This includes:
- loops that executes a set amount of times (including nested)
- for example,
for i in range(10)
- for example,
- sequence of basic operations
- conditionals (if all cases take O(1))
Common loops
- non nested loops that executes based on input size:
- e.g.
for i in range(n)
- e.g.
- nested loops that only have statements inside:
- e.g.
for i in range(n): for j in range(m):
- e.g.
- nested loops where the inner loop stops based on instead of :
- e.g.
for i in range(n): for j in range(n):
- e.g.
Functions and Procedure Calls
- series of functions / procedure calls: take the highest time complexity
- when a loop is applied, apply the product rule: if a loop iterates times and the inside call has complexity, then overall the loop will have complexity .
Difficult Examples
int x = 0;
int y = 0;
for(int i = 1; i <= n; i = i * 3) {
x = x + 5;
x++;
y = y * y;
}
x = 0
y = 0
i=1
while i<=n:
x += 5
x+=1
y *= y
i *= 3
Answer
Loop clearly does work per iteration, so focus on how many iterations there will be. will take on values for some arbitrary iteration of the loop . The loop stops when . Hence, , leading to the loop stopping when . Therefore, the number of iterations is only .
int y = 0;
for(int j = 1; j*j <= n; j++)
y = y*y*50;
y = 0
while j**2 <=n:
y = y*y*50
j++
Answer
Following the logic from the last answer, the loop stops when , or . Hence the time complexity is .
int b = 0;
for(int i = n; i > 0; i--) {
for(int j = 0; j < i; j++) {
b = b + 4;
}
}
Answer
For nested loops, you count the number of total iterations done. Hence, in this case, the inner loop will iterate times, in total which is .
int y = 1;
int j = 0;
for(j = 1; j <= 2 * n; j = j + 2)
y = y + j;
int s = 0;
for(int i = 1; i <= j; i++)
s++;
Answer
First loop: note how for some arbitrary in an iteration. Hence, for the loop to terminate, leading to . Hence, the loop runs in time.
Second loop: is now , which is still dependent on input . Hence, the second loop runs in time as well.
Overall .
int b = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < i * n; j++) {
b = b + 5;
}
}
Answer
Inner loop will iterate times, which is . Hence, time complexity.
int x = 0;
for(int i = 1; i <= n; i = i * 3) {
if (i%2 != 0) {
for(int j = 0; j < i; j++) {
x++;
}
}
}
Answer
Due to binary factorisation, the condition is always true, meaning the inner loop always runs. The inner loop then runs times, which is the sum of a geometric series, equal to , which gives the time complexity .
int t = 0;
for(int i = 1; i <= n; i++)
for(int j = 0; j*j < 4*n; j++)
for(int k = 1; k*k <= 9*n; k++)
t++;
Answer
First loop is , second loop is , third loop is . Total work done is .
int y = 0, s = 0;
for(int j = 0; j < n + 1; j++) {
y=y+j;
}
for(int i = 1; i <= y; i++) {
s++;
}
Answer
First loop has time complexity , resulting in . Hence, second loop has time complexity , having total time complexity of . In a sequence of statements, always pick the largest time complexity.
int i=1, z=0;
while(z < n*(n+1)/2)
{
z+=i; i++;
}
Answer
Intuitively you should note that the top is the sum of the series from 1 to n, whilst the bottom sums it up, making the time complexity
int a = 0;
int k = n*n*n;
while(k > 1)
{
for (int j=0; j<k; j++) {
a--;
}
k = k/2;
}
Answer
Don’t be fooled by the division by two, count the iterations. The inner loop would have iterated times. Hence the time complexity is actually .