Question

What is the time-complexity of the following code? Show how you derive the solution. for(i =...

What is the time-complexity of the following code? Show how you derive the solution.

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

for (j = 1; j < 2n; j++ ) {

for(k = 1; k < n; k *= 2) {

m += j;

}

}

}

Homework Answers

Answer #1
for(i = 1; i < 3^n; i++)
Values of i in this loop are 1,2,3,4,....,
So, Number of values for i = 

for(j=1; j < 2^n; j++)
Values of j in this loop are 1,2,3,4,....,
So, Number of values for j = 

for(k=1; k < n; k*=2)
Values of i in this loop are 1,2,4,8,....,n
So, Number of values for k = log(n)

Time complexity 
= O(Product of number of values for each loop variable) 
= O( log(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
What is the time-complexity of the following code? Explain, please. for(i = 1; i < 3n;...
What is the time-complexity of the following code? Explain, please. for(i = 1; i < 3n; i++){ for(j=1; j < 2n; j++){ for(k=1; k < n; k*=2){ m += j; } } }
What is the tilde time complexity for the following code fragments: 1) int sum = 0;...
What is the tilde time complexity for the following code fragments: 1) int sum = 0; for (i = n; i > 0; i /= 2) for (j = 0; j < i; j++) sum++; 2) int sum = 0; for (int i = 0; i < n; i++) for (int j = 0; j < i; j+=2) sum++;
What is the time complexity of the following code? (in big-O notaion) sum = 0 count...
What is the time complexity of the following code? (in big-O notaion) sum = 0 count = 0 var1 = 0 for: i=1 to n{ sum = sum+i for j=1 to [(3i+2i)(5n)(2i)]{ var1 = sum+count} } for m = i to j*j{ sum = sum*sum var2 = sum - (count+3)}
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm that is executed most frequently. Express the number of times it is executed as a function of N. Convert this expression into the Big-O notation. A. For each of the three fragments of code, what is its worst-case time complexity, in the form "O(…)". (Use the given solution to the first problem as a model)                 //----------------- This is a sample problem – solved ------...
Show that the running time for the following segment of code is O(N3) without using the...
Show that the running time for the following segment of code is O(N3) without using the rule for loop. Make sure to count each statement that takes one unit of time. sum = 0; for( i = 0; i < n; i++ ) for( j = 0; j < n; j++ )             for( k = 0; k < n; k++ ) sum++;
What is the time complexity of the following segment of code assuming that the function temp...
What is the time complexity of the following segment of code assuming that the function temp takes time O(n)? ... ... x = n y = n while y > 1:      print(x)      temp(x)      y = y / 2      x = x – 1 temp(x) … …
find time complexity in terms of n? use big-O i,j,k,x =0; for i=0; i<=n; i++; for...
find time complexity in terms of n? use big-O i,j,k,x =0; for i=0; i<=n; i++; for j=0, j<=5; j++; for k=5; k<=n; k=k*5; x = x*n;
1. For each pseudo-code below derive the simplified asymptotic running time in Q(?) notation. (1) for...
1. For each pseudo-code below derive the simplified asymptotic running time in Q(?) notation. (1) for 1 <-- .. n do j <- 2    while j <= n do j <-- j*j (2) for i <-- 1 .. n do j <-- 1     while j*j <= i do j <-- j + 1
Match the following. a) Completeness i) How long does it take to find a solution b)Time...
Match the following. a) Completeness i) How long does it take to find a solution b)Time Complexity ii) How much memory need to perform the search. c) Space Complexity iii) Is the strategy guaranteed to find the solution when there in one. Explain A) a-iii, b-ii, c-i B) a-i, b-ii, c-iii C) a-iii, b-i, c-ii D) a-i, b-iii, c-ii The number of comparisons done by sequential search of N elements in an array is ……………… A) (N/2)+1 B) (N+1)/2 C)...
Analyze the following code and provide a "Big-O" estimate of its running time in terms of...
Analyze the following code and provide a "Big-O" estimate of its running time in terms of n. Explain your analysis. for ( int i = n; i > 0; i /= 2 ) { for ( int j = 1; j < n; j += 2 ) { for ( int k = 0; k < n; k += 2 ) { ... // constant number of operations } } }
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT