Question

Assume evaluating a function f(n) in the pseudocode below takes theta(n) time. i = 1; sum...

Assume evaluating a function f(n) in the pseudocode below takes theta(n) time.
i = 1;
sum = 0;
while (i <= n)
do if (f(i) > k)
then sum += f(i);
i = 2*i;
What is the

Homework Answers

Answer #1
i = 1;
sum = 0;
while (i <= n)
do 
    if (f(i) > k)
    then 
        sum += f(i);
    end if
i = 2*i;
end while


i value is increased by a factor of 2 through each iteration
so, i values are 1, 2, 4, 8, ..., n/4, n/2, n
so, f(i) values are 1, 2, 4, 8, ..., n/4, n/2, n
so, total time complexity is 1 + 2 + 4 + 8 + ..., n/4 + n/2 + n
flip this expression
=>  n + n/2 + n/4 + ... + 1
=>  n(1 + 1/2 + 1/4 + 1/8 + ...)
we know that 1 + 1/2 + 1/4 + 1/8 + ... <= 2
so, n(1 + 1/2 + 1/4 + 1/8 + ...) <= 2n
hence time complexity is O(n)

Answer: O(n)
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Give pseudocode for a Theta(n)-time algorithm that returns the 50 largest values in an array. You...
Give pseudocode for a Theta(n)-time algorithm that returns the 50 largest values in an array. You may assume the array has at least 50 values.
Consider the following pseudocode segment. x ← 3 for i  {1, 2,   , n} do      for j  {1,...
Consider the following pseudocode segment. x ← 3 for i  {1, 2,   , n} do      for j  {1, 2,   , n} do      x ← x + 5      for k  {1, 2, 3, 4, 5} do      x ← x + k + 1 What is the value of x after this segment runs?
Consider the following function: double arrayavg(int a[], int n){ int sum=0; for(int i=0; i!=n; ++i){ sum+=a[i];...
Consider the following function: double arrayavg(int a[], int n){ int sum=0; for(int i=0; i!=n; ++i){ sum+=a[i]; } return (double)sum/n; } Rewrite the function to eliminate the array subscripting (a[i]), using pointer arithmatic instead. Write a program that reads a line of input and checks if it is a palindrome (ignoring spaces) Write a program that takes the name of a file as a command line argument, and prints the contents of the file (similar to the linux 'cat' program). Write...
1.Write a function which takes in a dictionary are returns the sum of the keys plus...
1.Write a function which takes in a dictionary are returns the sum of the keys plus the sum of the values, but only if all the keys and all the values are integers. Otherwise it returns False. >>>f({'a':1,'b':4,'c':7,'d':11}) False >>>f({1:2,3:4,5:6,7:8}) 36 2.Write a function to quickly compute the recaman sequence. 3. The Hofstadter Conway sequence is defined by a(1)=a(2)=1 and (for n>2 by) a(n)=a(a(n-1))+a(n-a(n-1)). Write a function to quickly compute this sequence. >>> [hc(i) for i in range(1,20)] [1, 1,...
1.) Answer the following questions based on the following pseudocode for the function “foo”: Algorithm 1:...
1.) Answer the following questions based on the following pseudocode for the function “foo”: Algorithm 1: foo mystery() foreach i ←1 to n do mystery() foreach j ←1 to i do (A) Determine the exact number of times mystery() is called in terms of n. (B) Assume the mystery function is in O(log(n) ∗ n ^2 ). Determine the complexity of “foo” in O-notation. if i ≤ j then mystery()
Question 4.5 Consider the fragment in Algorithm 4.8 shown below. Algorithm 4.8 sum = 0; for...
Question 4.5 Consider the fragment in Algorithm 4.8 shown below. Algorithm 4.8 sum = 0; for (i =1; i <=f(n); i ++) sum += i; Here f(n) is a function call. Give a simple and tight Big-Oh upper bound on the running time of Algorithm 4.8, as a function of n, on the assumption that (a) the running time of f(n) is O(n), and the value of f(n) is n!. (b) the running time of f(n) is O(n), and the...
Consider the following function : F 1 = 2, F n = (F n-1 ) 2...
Consider the following function : F 1 = 2, F n = (F n-1 ) 2 , n ≥ 2 i. What is the complexity of the algorithm that computes F n using the recursive definition given above. ii. Describe a more efficient algorithm to calculate F n and give its running time.
Solve f(n) as a function of n using the homogeneous equation and given the conditions below:...
Solve f(n) as a function of n using the homogeneous equation and given the conditions below: f(0) = 0;    f(1) = 1; f(2) = 4; f(n) = 2 f(n-1) - f(n-2) + 2; n > 2
1.Consider the function represented in sum of product form as F(x, y, z)=∑(0,1,4,6). Write the function...
1.Consider the function represented in sum of product form as F(x, y, z)=∑(0,1,4,6). Write the function F in product of sum form and minimize it using K-map. 2. Design a digital circuit (with minimum number of gates) with input ? = ?3?2?1?0 and an output ?. The output F should be 1 if A is divisible by either 2 or 3.
Given: function f = fibonacci(n) % FIBONACCI Fibonacci sequence % f = FIBONACCI(n) generates the first...
Given: function f = fibonacci(n) % FIBONACCI Fibonacci sequence % f = FIBONACCI(n) generates the first n Fibonacci numbers. f = zeros(n,1); f(1) = 1; f(2) = 2; for k = 3:n f(k) = f(k-1) + f(k-2); end AND function f = fibnum(n) if n <=1 f =1; else f = fibnum(n-1) + fibnum(n-2); end In Matlab, modify fibonacci.m and fibnum.m to compute the following sequence. dn = 0, n<=0 d1 = 1, d2 = 1 and, for n >...