0% found this document useful (0 votes)
3 views15 pages

2. Problem on Complexity Analysis

The document discusses the time and space complexities of various code snippets, providing detailed explanations for each case. It covers complexities ranging from O(N + M) to O(3^n) and includes examples to illustrate the reasoning behind each complexity. The document serves as a comprehensive guide for understanding algorithmic efficiency in terms of time and space.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views15 pages

2. Problem on Complexity Analysis

The document discusses the time and space complexities of various code snippets, providing detailed explanations for each case. It covers complexities ranging from O(N + M) to O(3^n) and includes examples to illustrate the reasoning behind each complexity. The document serves as a comprehensive guide for understanding algorithmic efficiency in terms of time and space.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

TIME AND SPACE COMPLEXITY

06 February 2024 23:10


1. What is the time, and space complexity of the following code :

Ans : O(N + M) time, O(1) space

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).

2. What is the time complexity of the following code :

Ans : O(N*N)

Explaination : The above code runs total no of times


= N + (N – 1) + (N – 2) + … 1 + 0
= N * (N + 1) / 2
= 1/2 * N2 + 1/2 * N
O(N2) times.

3. What is the time complexity of the following code :

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).

4. What is the time complexity of the following code :

Ans : O(log N)
Explanation : We have to find the smallest x such that ‘(N / 2x )< 1 OR 2x > N’
x = log(N)

5. What will be the time complexity of the following code ?

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.

6. What will be the time complexity of the following code ?

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).

7. What is the time complexity of the following code.


Ans : Time complexity = O(√n).

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

When S>=n , then loop will stop at kth iterations,


⇒ S>=n ⇒ S=n
⇒ 2 + 2 + 3 + 4 + ……+ k = n
⇒ 1 + (k * (k + 1))/2 = n
⇒ k2 = n
k = √n

Hence, the time complexity is O(√n).


8. What is the time complexity of the following code :

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.

9. What is the time complexity of the following code :

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.

10. What is the time complexity of the following code :

Ans : Time complexity = O(√n).

Explanation – Let k be the no. of iteration of the loop.

i i*i
1 1
2 2
2
3 3
2
4 4
2
… …
k k
2

⇒ The loop will stop when i * i >=n i.e., i*i=n


⇒ i*i=n ⇒ k2 = n
⇒k =√n
Hence, the time complexity is O(√n).

11. What is the time complexity of the following code :

Ans : Time complexity = O(logxn).

Explanation – Let k be the no. of iteration of the loop.

no. of itr i=i*x


1 1*x=x
2 x*x=x
2
3 x
2
*x=x
3
… …
k x
k-1
*x= x
k
⇒ The loop will stop when i>=n ⇒ xk = n
⇒ xk = n (Take log both sides)
⇒ k = logxn
⇒Hence, time complexity is O(logxn).

12. What is the time complexity of the following code :

Ans : Time complexity = O(n2log2n).

Explanation – Time complexity of 1st for loop = O(n/2) ⇒ O(n).


Time complexity of 2nd for loop = O(n/2) ⇒ O(n).
Time complexity of 3rd for loop = O(log2n).

Hence, the time complexity of function will become O(n2log2n).

13. What is the time complexity of the following code :

Ans : Time complexity = O(nlogn).


Explanation – Time complexity of 1st for loop = O(n). 2nd loop executes n/i times
for each value of i.
Its time complexity is O(∑ni=1 n/i) ⇒ O(logn).
Hence, time complexity of function = O(nlogn).

14. What is the time complexity of the following code :

Ans : Time complexity = O(n2).

Explanation – Time complexity of 1st for loop = O(n/3) ⇒ O(n).


Time complexity of 2nd for loop = O(n/4) ⇒ O(n).
Hence, the time complexity of function will become O(n2).

15. What is the time complexity of the following code :


Ans : Time complexity = O(log2n).

Explanation – In each iteration , i become twice (T.C=O(logn)) and j become half


(T.C=O(logn)). So, time complexity will become O(log2n).
We can convert this while loop into for loop.
for( int i = 1; i < n; i = i * 2)
for( int j=n ; j > 0; j = j / 2).

Time complexity of above for loop is also O(log2n).

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.

17. Find the complexity of the below recurrence :


Solution : Let us solve using substitution.
T(n) = 3T(n-1)
= 3(3T(n-2))
= 32T(n-2)
= 33T(n-3)


= 3nT(n-n)
= 3nT(0)
= 3n
This clearly shows that the complexity of this function is O(3n).

18. Find the complexity of the recurrence :

Solution : Let us try solving this function with substitution.


T(n) = 2T(n-1) – 1
= 2(2T(n-2)-1)-1
= 22(T(n-2)) – 2 – 1
= 22(2T(n-3)-1) – 2 – 1
= 23T(n-3) – 22 – 21 – 20
…..
…..
= 2nT(n-n) – 2n-1 – 2n-2 – 2n-3
….. 22 – 21 – 20
= 2n – 2n-1 – 2n-2 – 2n-3
….. 22 – 21 – 20
= 2n – (2n-1)
[Note: 2n-1 + 2n-2 + …… + 20 = 2n – 1]
T(n) = 1
Time Complexity is O(1).

Note that while the recurrence relation looks exponential


he solution to the recurrence relation here gives a different result.
19. Find the complexity of the below program :

Solution : Consider the comments in the following function.

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 :

Solution : Consider the comments in the following function.

Time Complexity: O(n log2n).

You might also like