Module Name - Algorithm
Analysis
EditEdit
MasterMaster
Class - Day 1
texttext
stylesstyles
Practice Questions
1) What is the time complexity of the following code :
int a = 0, i = N;
while (i > 0) {
a += i;
i /= 2;
}
Practice Questions
Ans : O(log N)
We have to find the smallest x such that `N / 2^x < 1 OR 2^x > N`
x = log(N)
Practice Questions
2) Given two positive integers n and k, print all increasing
sequences of length k such that the elements in every sequence
are from first n natural numbers.
Practice Questions
Practice Questions
Output = 1 2 , 1 3 , 2 3
Homework questions
1) What is time complexity of following code :
int count = 0;
for (int i = N; i > 0; i /= 2) {
for (int j = 0; j < i; j++) {
count += 1;
}
Homework questions
2) Write a program to print all possible combinations of k
elements in the given integer array of size n.
Note : array does not contain duplicates.
Module Name - Algorithm
Analysis
EditEdit
MasterMaster
Class - Day 2
texttext
stylesstyles
Practice Questions
1) What is the time complexity of the following code :
int i, j, k = 0;
for (i = n/2; i <= n; i++) {
for (j = 2; j <= n; j = j * 2) {
k = k + n/2;
}
}
Practice Questions
If you notice, j keeps doubling till it is less than or equal to n.
Number of times, you can double a number till it is less than n
would be log(n).
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) `
Practice Questions
2) Given a singly linked list, delete node at k-th position using
recursion.
Practice Questions
Practice Questions
Practice Questions
Output = Modified Linked List: 20 25 1 53 62 21 3
Homework questions
1) Which of the given options provides the increasing order of
complexity of functions f1, f2, f3 and f4:
f1(n) = 2^n
f2(n) = n^(3/2)
f3(n) = nLogn
f4(n) = n^(Logn)
Homework questions
2) Given a number n, print a pattern without using any loop. We
basically first reduce 5 from n one by one until we reach a
negative or 0. After we reach 0 or negative, we one add 5 until
we reach n. For eg-
Input: n = 10
Output: 10, 5, 0, 5, 10
#LifeKoKaroLift
Thank
you!