2. Problem on Complexity Analysis
2. Problem on Complexity Analysis
Explanation : The first loop is O(N) and the second loop is O(M). Since N and M are
independent variables, so we can’t say which one is the leading term. Therefore
Time complexity of the given problem will be O(N+M).
Since variables size does not depend on the size of the input, therefore Space
Complexity will be constant or O(1).
Ans : O(N*N)
Ans : O(nLogn)
Explanation : If you notice, j keeps doubling till it is less than or equal to n. Several
times, we can double a number till it is less than n would be log(n).
Let’s take the examples here.
for n = 16, j = 2, 4, 8, 16
for n = 32, j = 2, 4, 8, 16, 32
So, j would run for O(log n) steps.
i runs for n/2 steps.
So, total steps = O(n/ 2 * log (n)) = O(n*logn).
Ans : O(log N)
Explanation : We have to find the smallest x such that ‘(N / 2x )< 1 OR 2x > N’
x = log(N)
Ans : O(logkn)
Explanation : Because the loop will run kc-1 times, where c is the number of
times i can be multiplied by k before i reaches n. Hence, kc-1 =n. Now to find the
value of c we can apply log and it becomes logkn.
Ans : n(n-1)
Explanation : First for loop will run for (n) times and another for loop will be run
for (n-1) times as the inner loop will only run till the range i which is 1 less than n ,
so overall time will be n(n-1).
Explanation – We can define the ‘S’ terms according to the relation Si = Si-1 + i.
Let k is the total number of iterations taken by the program.
i S
1 1
2 2
3 2+2
4 2+2+3
… …
k 2 + 2 + 3 + 4 + ……+ k
Ans : Time complexity = O(1) in best case and O(n) in worst case.
Explanation – This program contains if and else condition. Hence, there are 2
possibilities of time complexity. If the value of n is less than 5, then we get
only Good Morning as output and its time complexity will be O(1).
But, if n>=5, then for loop will execute and time complexity becomes O(n), it is
considered as worst case because it takes more time.
Ans : Time complexity = O(1) in best case and O(max(a, b)) worst case.
Explanation – If the value of a and b are the same, then while loop will not be
executed. Hence, time complexity will be O(1).
But if a!=b, then the while loop will be executed. Let a=16 and b=5;
no. of iterations a b
1 16 5
2 16-5=11 5
3 11-5=6 5
4 6-5=1 5
5 1 5-1=4
6 1 4-1=3
7 1 3-1=2
8 1 2-1=1
For this case, while loop executed 8 times (a/2 ⇒16/2 ⇒8).
If a=5 and b=16, then also the loop will be executed 8 times. So we can say that
time complexity is O(max(a/2,b/2)) ⇒ O(max(a, b)), it is considered as worst case
because it takes more time.
i i*i
1 1
2 2
2
3 3
2
4 4
2
… …
k k
2
16. Consider the following code, what is the number of comparisons made in the
execution of the loop ?
Ans : ceil(log2n)+1.
Explanation – Let k be the no. of iteration of the loop. After the kth step, the
value of j is 2k. Hence, k=log2n. Here, we use ceil of log2n, because log2n may be in
decimal. Since we are doing one more comparison for exiting from the loop.
So, the answer is ceil(log2n)+1.
Time Complexity : O(n), Even though the inner loop is bounded by n, but due to the
break statement, it is executing only once.
20. Find the complexity of the below program :