Asymptotic Notation · Algorithms
40 questions · asked in 21 of 27 years (2000–2026)
MCQ 37 · MSQ 2 · NAT 11-mark 27 · 2-mark 13
Questions 26–30 of 40
int IsPrime(n)
{
int i,n;
for(i=2;i<=sqrt(n);i++)
if(n%i == 0)
{printf("Not Primen"); return 0;}
return 1;
}
Let T(n)denote the number of times the for loop is executed by the program on
input n. Which of the following is TRUE?Answer: (C)
Upper bound: the loop never runs more than about times, so . For a prime no divisor is found and it runs about times.
Lower bound: for every the loop runs at least once, so . If is even, then at the first pass () and the function returns, so for all such .
Because of these even inputs, does not hold, which rules out (A) and (B). In (C), both parts are true.
Answer: (C)
int DoSomething (int n)
{
if (n <= 2)
return 1;
else
return (DoSomething (floor(sqrt(n))) + n);
}Answer: (D)
Expand: .
The calls stop when the argument reaches or less, so we need . Take of both sides: , so and . The floor in and the last call change this only by a constant, so .
Check with a number: for the arguments are . That is square roots and calls in all, and .
Answer: (D)
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?Answer: (A)
Claim: .
If , then .
If , then .
So the first argument at least halves every two calls, and the number of calls is at most about , which is .
This is also reached: for consecutive Fibonacci numbers , each step only goes one Fibonacci number down, so it takes about calls. So the worst case is .
Answer: (A)
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?Answer: (C)
Starting at , the values added are (integer division), and the loop stops when becomes . That is about terms:
using the sum of a geometric series. Integer division loses less than per term, so less than in total, which does not change the order. So .
Answer: (C)
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), , 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:Answer: (B)
Without storing values, the calls form this tree. The loop runs for , so the last child of the root is , and the labels going down that last branch are .
Many calls repeat. Let be the number of calls: gives . The deepest chain of calls is , so the stack depth is . So space is linear and time is exponential.
Take . Then , in general for . There are calls and the stack depth is .
Now store each in a one-dimensional table of doubles the first time it is computed. The plan is:
- First, look in the table for the value of the call we need.
- If it is there, return it without any recursive call.
- If not, run the loop of recursive calls, store the result, and return it.
The figure below shows the same idea.
Here is the code:
#define UNUSED 0.5
double *memo_table; // size n+1, every entry set to UNUSED, then memo_table[0] = 1.0
double foo(int n) {
int i;
double sum;
if (memo_table[n] != UNUSED) {
return memo_table[n];
} else {
sum = 0.0;
for (i = 0; i < n; i++) {
sum += foo(i);
}
return memo_table[n] = sum;
}
}
The loop calls in order, so when runs, are already stored and its own calls return at once. So the recursion is only a few levels deep, which is stack space. The time drops to , because each of the values is computed once with a loop of at most steps.
Space = table + stack = . It cannot be , since the table keeps values. and are far more than is needed.
Answer: (B)
Keep practising