Top MCQs on Complexity Analysis of Algorithms with Answers (original) (raw)

What is time complexity of fun()?

C++ `

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; }

C

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; }

Java

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; }

Python

def fun(n): count = 0 i = n while i > 0: for j in range(i): count += 1 i //= 2 return count

C#

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; }

JavaScript

function fun(n) { let count = 0; for (let i = n; i > 0; i = Math.floor(i / 2)) for (let j = 0; j < i; j++) count += 1; return count; }

`

What is the time complexity of fun()?

C++ `

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; }

C

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; }

Java

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; }

Python

def fun(n): count = 0 for i in range(n): for j in range(i, 0, -1): count += 1 return count

C#

int Fun(int n) { int count = 0; for (int i = 0; i < n; i++) for (int j = i; j > 0; j--) count++; return count; }

JavaScript

function fun(n) { let count = 0; for (let i = 0; i < n; i++) for (let j = i; j > 0; j--) count++; return count; }

`

The recurrence relation capturing the optimal time of the Tower of Hanoi problem with n discs is. (GATE CS 2012)

O( n2 ) is the worst case time complexity, so among the given options it can represent :-

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)

What is the time complexity of the below function?

C `

void fun(int n, int arr[]) { int i = 0, j = 0; for (; i < n; ++i) while (j < n && arr[i] < arr[j]) j++; }

`

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:

C `

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

What does it mean when we say that an algorithm X is asymptotically more efficient than Y?

Consider the following functions:

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

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) = [Tex]\\Omega [/Tex](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) = [Tex]\\Omega [/Tex](f(n))

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

C `

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) [Tex]\\theta [/Tex](log(n))
(B) [Tex]\\Omega [/Tex](n)
(C) [Tex]\\theta [/Tex](log(log(n)))
(D) [Tex]\\theta [/Tex](sqrt(n))

There are 119 questions to complete.

Take a part in the ongoing discussion