0% found this document useful (0 votes)
19 views

Assignment

Uploaded by

ro6162
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Assignment

Uploaded by

ro6162
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Q1.

Time-Complexity-1Solved

Stuck somewhere?
Ask for help from a TA and get it resolved.
Get help from TA.

What is the time, space complexity of following code:

int a = 0, b = 0;
for (i = 0; i < N; i++) {
a = a + rand();
}
for (j = 0; j < M; j++) {
b = b + rand();
}
Choose the correct answer from below:
O(N * M) time, O(1) space
O(N + M) time, O(N + M) space
O(N + M) time, O(1) space
O(N * M) time, O(N + M) space

Q2. Time-Complexity-2Solved

Stuck somewhere?
Ask for help from a TA and get it resolved.
Get help from TA.

What is the time complexity of the following code :

 C++
 Python

void solve(int n){


for(int i = 0; i < n; i++){
for(int j=0; j < i / 2; j++){
// O(1) operation
}
}
}
def solve():
for i in range(n):
for j in range(i // 2):
# O(1) operation
Choose the correct answer from below:
O(N)
O(N*log(N))
O(N * Sqrt(N))
O(N*N)

Q3. Time-Complexity-3Solved

Stuck somewhere?
Ask for help from a TA and get it resolved.
Get help from TA.

Python

k = 0
for i in range(n//2, n+1):
j = 2
while j<=n:
k = k + n//2
j = j * 2

What is the time complexity of following code:

Choose the correct answer from below:


O(n)
O(nLogn)
O(n^2)
O(n^2Logn)

Q4. Time-Complexity-5Unsolved

Stuck somewhere?
Ask for help from a TA and get it resolved.
Get help from TA.
What is the time complexity of following code:

int a = 0, i = N;
while (i > 0) {
a += i;
i /= 2;
}
Choose the correct answer from below:
O(N)
O(Sqrt(N))
O(N / 2)
O(log N)

Q5. Time-Complexity-6Unsolved

Stuck somewhere?
Ask for help from a TA and get it resolved.
Get help from TA.

The complexity of Binary search algorithm is

Choose the correct answer from below:


O(n)
O(log n)
O(n^2)
O(n log n)

Q6. Time-Complexity-8Unsolved

Stuck somewhere?
Ask for help from a TA and get it resolved.
Get help from TA.

If for an algorithm time complexity is given by O(1) then complexityof it is:


Choose the correct answer from below:
constant
polynomial
exponential
none of the mentioned

Q7. Time-Complexity-9Unsolved

Stuck somewhere?
Ask for help from a TA and get it resolved.
Get help from TA.

If for an algorithm time complexity is given by O(log2n) then complexity will:

Choose the correct answer from below:


constant
polynomial
exponential
none of the mentioned

Q7. Time-Complexity-9Unsolved

Stuck somewhere?
Ask for help from a TA and get it resolved.
Get help from TA.

If for an algorithm time complexity is given by O(log2n) then complexity will:

Choose the correct answer from below:


constant
polynomial
exponential
none of the mentioned
Q8. Time-Complexity-10Unsolved

Stuck somewhere?
Ask for help from a TA and get it resolved.
Get help from TA.

If for an algorithm time complexity is given by O(n) then complexityof it is:

Choose the correct answer from below:


constant
linear
exponential
none of the mentioned

Q9. Time-Complexity-12Unsolved

Stuck somewhere?
Ask for help from a TA and get it resolved.
Get help from TA.

If for an algorithm time complexity is given by O((3/2)^n) then complexity will:

Choose the correct answer from below:


constant
quardratic
exponential
none of the mentioned

Q10. Time-Complexity-19Unsolved

Stuck somewhere?
Ask for help from a TA and get it resolved.
Get help from TA.

The complexity of linear search algorithm is


Choose the correct answer from below:
O(n)
O(log n)
O(n2)
O(n log n)

Q11. Time-Complexity-22Unsolved

Stuck somewhere?
Ask for help from a TA and get it resolved.
Get help from TA.
What is the time complexity of 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;
}
}
Choose the correct answer from below:
O(n)
O(nLogn)
O(n^2)
O(n^2Logn)

Q12. Time Complexity - 1.0Unsolved

Stuck somewhere?
Ask for help from a TA and get it resolved.
Get help from TA.

What will be the Time Complexity if solve() function is called?

 C++
 Java
 Python
void solve(int n){
int i = n;
while(i > 0){
if(i%2 == 0){
for(int j = 1; j <= n * n; j += 2){
// O(1) operation
}
}
i /= 2;
}
}
public void solve(int n){
int i = n;
while(i > 0){
if(i%2 == 0){
for(int j = 1; j <= n * n; j += 2){
// O(1) operation
}
}
i /= 2;
}
}
def solve():
i = n
while i>0:
if i%2==0:
for j in range(1,n*n+1,2):
#O(1) operation
i = i//2
Choose the correct answer from below:
O(logn)
O(n^3)
O(n^2 logn)
O(nlogn)
None of the above

Q13. Time Complexity - 3.0Unsolved

Stuck somewhere?
Ask for help from a TA and get it resolved.
Get help from TA.

What is the Time Complexity of the below Code?


 C++
 Java
 Python

void solve(){
int i = 1;
while(i < n){
int x = i;
while(x--){
// O(1) operation
}
i++;
}
}
public void solve(){
int i = 1;
while(i < n){
int x = i;
while(x-- > 0){
// O(1) operation
}
i++;
}
}
def solve():
i = 1
while i < n:
x = i
while x > 0:
# O(1) operation
x -= 1
i += 1
Choose the correct answer from below:
O(nlogn)
O(n)
O(n sqrt(n))
O(n^2)
None of the above

You might also like