Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Questions on Time Complexity, Exercises of Algorithms and Programming

Time Complexity very important questions are given here. Very important for interviews.

Typology: Exercises

2020/2021

Uploaded on 06/14/2021

rishi-chandak
rishi-chandak 🇮🇳

1 document

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Quiz on Divide And Conquer Answers with Explanation wherever possible
Q1) Let T(n)= T(n-1) + 1/n. Then T(n) is:
a. Ѳ (1)
b. Ѳ (log n)
c. Ѳ (log log n)
d. Ѳ (n)
Answer is b O(logN)
Explanation: By unfolding
T(n)=T(n−1)+1n=T(n−2)+1n+1n−1==T(0)+∑k=1n1kT(n)=T(n−1)+1n=T(n−2)+1n+1n−1==T(0)+∑k
=1n1k
Now we can easily approximate the sum on the RHS using that
∑k=1n1k≤1+∫n11xdx=1+logn−log1=1+logn∑k=1n1k≤1+∫1n1xdx=1+logn−log1=1+logn
Therefore T(n)≡O(logn)
Q2) Which of the following algorithms is NOT a divide & conquer algorithm by nature?
a. Heap Sort
b. Euclidean algorithm to compute the greatest common divisor
c. Cooley-Tukey fast Fourier transform
d. Quick Sort
Answer is a) Heap Sort
Q3) Consider the following function
find (int n)
{
if (n < 2 ) then return;
else
{
sum= 0;
for (i= 1; i ≤ 4; i++) find (n2);
for (i=1; i≤ n*n; i++) sum= sum + 1;
}
}
Assume that the division operation takes constant time and “sum” is global variable. What is the
time complexity of “find (n)” ?
a. n^2
b. n^3
c. n^2logn
d. None of these
pf3
pf4

Partial preview of the text

Download Questions on Time Complexity and more Exercises Algorithms and Programming in PDF only on Docsity!

Quiz on Divide And Conquer Answers with Explanation wherever possible Q1) Let T(n)= T(n-1) + 1/n. Then T(n) is: a. Ѳ (1) b. Ѳ (log n) c. Ѳ (log log n) d. Ѳ (n) Answer is b O(logN) Explanation: By unfolding T(n)=T(n−1)+1n=T(n−2)+1n+1n−1=⋯=T(0)+∑k=1n1kT(n)=T(n−1)+1n=T(n−2)+1n+1n−1=⋯=T(0)+∑k =1n1k Now we can easily approximate the sum on the RHS using that ∑k=1n1k≤1+∫n11xdx=1+logn−log1=1+logn∑k=1n1k≤1+∫1n1xdx=1+log n−log 1=1+log n Therefore T(n)≡O(logn) Q2) Which of the following algorithms is NOT a divide & conquer algorithm by nature? a. Heap Sort b. Euclidean algorithm to compute the greatest common divisor c. Cooley-Tukey fast Fourier transform d. Quick Sort Answer is a) Heap Sort Q3) Consider the following function find (int n) { if (n < 2 ) then return; else { sum= 0; for (i= 1; i ≤ 4; i++) find (n2); for (i=1; i≤ n*n; i++) sum= sum + 1; } } Assume that the division operation takes constant time and “sum” is global variable. What is the time complexity of “find (n)”? a. n^ b. n^ c. n^2logn d. None of these

Answer is c) n^2logn Explanation: Q4) Maximum Subarray Sum problem is to find the subarray with maximum sum. For example, given an array {12, - 13, - 5, 25, - 20, 30, 10}, the maximum subarray sum is 45. The naive solution for this problem is to calculate sum of all subarrays starting with every element and return the maximum of all. We can solve this using Divide and Conquer, what will be the worst case time complexity using Divide and Conquer. a. O(logN) b. O(N) c. O(NlogN) d. O(N^2) Answer is c O(NlogN) Explanation : The recurrence relation for the same is T(n) = 2T(n/2) + Θ(n) The above recurrence is similar to Merge Sort and can be solved either using Recurrence Tree