Complexity Analysis of Algorithms - P.R.K
Complexity Analysis of Algorithms - P.R.K
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;
count = count + 1;
return count;
Theta (n)
Theta (n2)
Theta (n*log(n))
Theta (n*(log(n*log(n))))
int i = 0, j = 0;
j++;
O(n)
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
if (n % m == 0)
return m;
n = n % m;
}
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))
if (n <= 1) return n;
return 2*fun1(n-1);
int fun2(int n)
if (n <= 1) return n;
(A) Theta(1)Theta(1)
(B) Theta(sqrtlogn)Theta(sqrtlogn)
(C) Theta(Logn/(LogLogn))Theta(Logn/(LogLogn))
(d) Theta(Logn)Theta(Logn)
int i;
double sum;
if (n = = 0) return 1.0;
else{
sum = 0.0;
return sum;
}
The space complexity of the above function is:
O(1)
O(n)
O(n!)
O(nn)
int i;
double sum;
if (n = = 0) return 1.0;
else
sum = 0.0;
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)
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;
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)
int i, j, k, p, q = 0;
p = 0;
++p;
++q;
return q;
}
Which one of the following is the time complexity for function fun1?
n3
n (logn)2
nlogn
nlog(logn)