0% found this document useful (0 votes)
15 views12 pages

Complexity Analysis of Algorithms - P.R.K

The document discusses various algorithms and their time complexities, presenting multiple choice questions related to complexity analysis. It covers topics such as time complexity of functions, recurrence relations, asymptotic behavior, and specific algorithmic scenarios. The document serves as a study guide for understanding algorithm efficiency and complexity in computer science.

Uploaded by

abhaysam123456
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views12 pages

Complexity Analysis of Algorithms - P.R.K

The document discusses various algorithms and their time complexities, presenting multiple choice questions related to complexity analysis. It covers topics such as time complexity of functions, recurrence relations, asymptotic behavior, and specific algorithmic scenarios. The document serves as a study guide for understanding algorithm efficiency and complexity in computer science.

Uploaded by

abhaysam123456
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Complexity Analysis of Algorithms

1. What is time complexity of fun()?


int fun(int n)
int count = 0;

for (int i = n; i > 0; i /= 2

for (int j = 0; j < i; j++)

count += 1;

return count;

 O(n2)
 O(n*log(n))
 O(n)
 O(n*log(n*Log(n)))
2. What is the time complexity of fun()?
int fun(int n)

int count = 0;

for (int i = 0; i < n; i++)

for (int j = i; j > 0; j--)

count = count + 1;

return count;

 Theta (n)
 Theta (n2)
 Theta (n*log(n))
 Theta (n*(log(n*log(n))))

THINK SUCCESS THINK AXIS


DSA by:- Er. PRACHI KHARE
3. The recurrence relation capturing the optimal time of the Tower
of Hanoi problem with n discs is. (GATE CS 2012)
 T(n) = 2T(n – 2) + 2
 T(n) = 2T(n – 1) + n
 T(n) = 2T(n/2) + 1
 T(n) = 2T(n – 1) + 1

4. O( n2 ) is the worst case time complexity, so among the given


options it can represent :-
 O( n )
 O( 1 )
 O ( nlogn )
 All of the above

5. Which of the given options provides the increasing order of


asymptotic complexity of functions f1, f2, f3, and f4?
f1(n) = 2n
f2(n) = n(3/2)
f3(n) = n*log(n)
f4(n) = nlog(n)

 f3, f2, f4, f1


 f3, f2, f1, f4
 f2, f3, f1, f4
 f2, f3, f4, f1

6. What is the time complexity of the below function?

void fun(int n, int arr[])

int i = 0, j = 0;

for (; i < n; ++i)

while (j < n && arr[i] < arr[j])

j++;

 O(n)

THINK SUCCESS THINK AXIS


DSA by:- Er. PRACHI KHARE
 O(n2)
 O(n*log(n))
 O(n*log(n)2)

7. In a competition, four different functions are observed. All the


functions use a single for loop and within the for loop, same set
of statements are executed. Consider the following for loops:
A) for(i = 0; i < n; i++)

B) for(i = 0; i < n; i += 2)

C) for(i = 1; i < n; i *= 2)

D) for(i = n; i <= n; i /= 2)
If n is the size of input(positive), which function is most efficient(if the task to
be performed is not an issue)?
 A
 B
 C
 D

8.What does it mean when we say that an algorithm X is


asymptotically more efficient than Y?
 X will be a better choice for all inputs
 X will be a better choice for all inputs except possibly small
inputs
 X will be a better choice for all inputs except possibly large inputs
 Y will be a better choice for small inputs

9.Consider the following functions:


f(n) = 2n
g(n) = n!
h(n) = nlog(n)

THINK SUCCESS THINK AXIS


DSA by:- Er. PRACHI KHARE
Which of the following statements about the asymptotic behavior of f(n), g(n),
and h(n) is true?
(A) f(n) = O(g(n)); g(n) = O(h(n))
(B) f(n) = Omega Omega (g(n)); g(n) = O(h(n))
(C) g(n) = O(f(n)); h(n) = O(f(n))
(D) h(n) = O(f(n)); g(n) = Omega Omega (f(n))

10.In the following C function, let n >= m.


int gcd(n, m)

if (n % m == 0)

return m;

n = n % m;

return gcd(m, n);

}
How many recursive calls are made by this function?
(A) theta theta (log(n))
(B) Omega Omega (n)
(C) theta theta (log(log(n)))
(D) theta theta (sqrt(n))

11.Consider the following functions


 f(n) = 3n^{\sqrt{n}}
 g(n) = 2^{\sqrt{n}{\log_{2}n}}
 h(n) = n!
Which of the following is true? (GATE CS 2000)
(A) h(n) is 0(f(n))
(B) h(n) is 0(g(n))
(C) g(n) is not 0(f(n))
(D) f(n) is 0(g(n))

12.Consider the following three claims:

THINK SUCCESS THINK AXIS


DSA by:- Er. PRACHI KHARE
I (n+k)m=θ(nm)(n+k)m=θ(nm) where k and m are constants
II 2n+1=O(2n) 2n+1=O(2n)
III 2^{2n+1} = O(2^n)
Which of these claims is correct? (GATE CS 2003)
 I and II
 I and III
 II and III
 I, II and III

13.Let s be a sorted array of n integers. Let t(n) denote the time


taken for the most efficient algorithm to determined if there are two
elements with sum less than 1000 in s. which of the following
statements is true? (GATE CS 2000)
a) t (n) is 0 (1)
b) n < t (n) < n log2n log2n
c) n log 2 n < t (n) < nchoose2 nchoose2
d) t (n) = nchoose2 nchoose2

14.Consider the following function,


int unknown(int n) {
int i, j, k = 0;
for (i = n/2; i <= n; i++)
for (j = 2; j <= n; j = j * 2)
k = k + n/2;
return k;
}
What is the time complexity of the function? (GATE CS 2013)
 n^2
 n logn
 n^3
 n^3 logn

15.Consider the following two functions. What are time complexities


of the functions?

THINK SUCCESS THINK AXIS


DSA by:- Er. PRACHI KHARE
int fun1(int n)

if (n <= 1) return n;

return 2*fun1(n-1);

int fun2(int n)

if (n <= 1) return n;

return fun2(n-1) + fun2(n-1);

 O(2^n) for both fun1() and fun2()


 O(n) for fun1() and O(2^n) for fun2()
 O(2^n) for fun1() and O(n) for fun2()
 O(n) for both fun1() and fun2()

16.What is the worst case time complexity of insertion sort where


position of the data to be inserted is calculated using binary search?
 N
 N*log(N)
 N2
 N*log(N)2

17.The tightest lower bound on the number of comparisons, in the


worst case, for comparison-based sorting is of the order of
 N
 N^2
 NlogN
 N(logN)^2

THINK SUCCESS THINK AXIS


DSA by:- Er. PRACHI KHARE
18.The number of elements that can be sorted
inTheta(logn)Theta(logn) time using heap sort is

(A) Theta(1)Theta(1)
(B) Theta(sqrtlogn)Theta(sqrtlogn)
(C) Theta(Logn/(LogLogn))Theta(Logn/(LogLogn))
(d) Theta(Logn)Theta(Logn)

19.Consider the following segment of C-code:


int j, n;
j = 1;
while (j <= n)
j = j*2;
The number of comparisons made in the execution of the loop for any n > 0
is: Base of Log is 2 in all options.
 CEIL(logn) + 2
 n
 CEIL(logn)
 FLOOR(logn) + 2

20.Consider the following C-program fragment in which i, j and n are


integer variables.
for (i = n, j = 0; i >0; i /= 2, j += i);
Let val(j) denote the value stored in the variable j after termination of the for
loop. Which one of the following is true?
(A) val(j) =thetatheta(logn)

(B) vaI(j) =thetatheta(sqrt(n))

(C) val(j) =thetatheta(n)

(D) val(j) =thetatheta(nlogn)


21. The minimum number of comparisons required to find the minimum and
the maximum of 100 numbers is ______________.
 148
 147
 146

THINK SUCCESS THINK AXIS


DSA by:- Er. PRACHI KHARE
 140

22.Consider the following pseudo code. What is the total number of


multiplications to be performed?
D=2
for i = 1 to n do
for j = i to n do
for k = j + 1 to n do
D=D*3

Half of the product of the 3 consecutive integers.



One-third of the product of the 3 consecutive integers.

One-sixth of the product of the 3 consecutive integers.

None of the above.

23.Consider the following C-function:
double foo (int n){

int i;

double sum;

if (n = = 0) return 1.0;

else{

sum = 0.0;

for (i = 0; i < n; i++)

sum += foo (i);

return sum;

}
The space complexity of the above function is:
 O(1)
 O(n)
 O(n!)
 O(nn)

24. Consider the following C-function:

THINK SUCCESS THINK AXIS


DSA by:- Er. PRACHI KHARE
double foo (int n)

int i;

double sum;

if (n = = 0) return 1.0;

else

sum = 0.0;

for (i = 0; i < n; i++)

sum += foo (i);

return sum;

}
Suppose we modify the above function foo() and store the values of foo (i), 0
< = i < n, as and when they are computed. With this modification, the time
complexity for function foo() is significantly reduced. The space complexity
of the modified function would be:
 O(1)
 O(n)
 O(n!)
 O(nn)

25.Two matrices M1 and M2 are to be stored in arrays A and B respectively.


Each array can be stored either in row-major or column-major order in
contiguous memory locations. The time complexity of an algorithm to
compute M1 × M2 will be
 best if A is in row-major, and B is in column- major order

THINK SUCCESS THINK AXIS


DSA by:- Er. PRACHI KHARE
 best if both are in row-major order
 best if both are in column-major order
 independent of the storage scheme

26. Let A[1, ..., n] be an array storing a bit (1 or 0) at each location, and f(m)
is a function whose time complexity is θ(m). Consider the following program
fragment written in a C like language:
counter = 0;

for (i = 1; i < = n; i++)

if (A[i] == 1)

counter++;

else {

f(counter);

counter = 0;

}
1
}
The complexity of this program fragment is
 Ω(n2)
 Ω(nlog n) and O(n2)
 θ(n)
 O(n)

27. The recurrence equation


T(1) = 1
T(n) = 2T(n - 1) + n, n ≥ 2
evaluates to a. 2n + 1- n - 2 b. 2n - n c. 2n + 1 - 2n - 2 d. 2n + n
 a
 b
 c
 d
THINK SUCCESS THINK AXIS
DSA by:- Er. PRACHI KHARE
28.Consider the following three claims
1. (n + k)m = Θ(nm), where k and m are constants
2. 2n + 1 = O(2n)
3. 22n + 1 = O(2n)
Which of these claims are correct ?
 1 and 2
 1 and 3
 2 and 3
 1, 2, and 3

29.The increasing order of following functions in terms of asymptotic


complexity
is:f1(n)=n0.999999log(n)f2(n)=10000000nf3(n)=1.000001nf4(n)=n2f1
(n)=n0.999999log(n)f2(n)=10000000nf3(n)=1.000001nf4(n)=n2
 f1(n); f4(n); f2(n); f3(n)
 f1(n); f2(n); f3(n); f4(n);
 f2(n); f1(n); f4(n); f3(n)
 f1(n); f2(n); f4(n); f3(n)

30.Consider the following C function.

int fun1 (int n)

int i, j, k, p, q = 0;

for (i = 1; i < n; ++i)

p = 0;

for (j = n; j > 1; j = j/2)

++p;

THINK SUCCESS THINK AXIS


DSA by:- Er. PRACHI KHARE
for (k = 1; k < p; k = k*2)

++q;

return q;

}
Which one of the following is the time complexity for function fun1?

 n3

 n (logn)2

 nlogn

 nlog(logn)

THINK SUCCESS THINK AXIS


DSA by:- Er. PRACHI KHARE

You might also like