Find the time complexity for the following function (the basic operation is the innermost loop body's assignment). function f(n) r ← 0 m ← 1 for i ← 1 to n do m ← 3 × m for j ← 1 to m do r ← r + j return r GROUP OF ANSWER(CHOOSE ONE CORRECT): Θ(n) Θ(n^3) Θ(3^n) Θ(nlogn) Θ(n^2)
Question
Find the time complexity for the following function (the basic operation is the innermost loop body's assignment).
function f(n) r ← 0 m ← 1 for i ← 1 to n do m ← 3 × m for j ← 1 to m do r ← r + j return r GROUP OF ANSWER(CHOOSE ONE CORRECT): Θ(n) Θ(n^3) Θ(3^n) Θ(nlogn) Θ(n^2)
Solution
The correct answer is Θ(3^n).
Here's why:
-
The outer loop runs n times, so it has a time complexity of O(n).
-
The inner loop runs m times, where m is tripled in each iteration of the outer loop. This means the inner loop runs 1 time in the first iteration, 3 times in the second iteration, 9 times in the third iteration, and so on.
-
The total number of times the inner loop runs across all iterations of the outer loop is the sum of a geometric series: 1 + 3 + 3^2 + 3^3 + ... + 3^(n-1).
-
The sum of this geometric series is (3^n - 1) / 2, which is O(3^n) because the highest power of n is the dominant term.
-
Because the inner loop is nested inside the outer loop, the total time complexity is the product of the time complexities of the outer and inner loops.
-
However, because O(3^n) grows much faster than O(n), the overall time complexity is dominated by the inner loop, making the time complexity of the entire function Θ(3^n).
Similar Questions
Find the time complexity for the following function (the basic operation is the innermost loop body's assignment). function f(n) r ← 0 m ← 1 for i ← 1 to n do m ← 3 × m for j ← 1 to m do r ← r + j return r
Consider the following recurrence relation: T(n)=3T(n/3)+n2/3,T(1)=1,T(0)=0 What is the complexity of T(n)? Options : Θ(n) Θ(nlogn) Θ(n^2/3) Θ(n^1.5)
Question #20What is the time complexity of this function / algorithm?void f(int n){ int i; int j; for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { printf("[%d] [%d]\n", i, j); } }}O(n!)O(2^n)O(1)O(n)O(nlog(n))O(n^2)O(log(n))Question #21What is the time complexity of removing at index n from an unsorted Python 3 list?O(n!)O(2^n)O(1)O(n)O(nlog(n))O(n^2)O(log(n))Question #22What is the time complexity of accessing the nth element on an unsorted array?O(n!)O(2^n)O(1)O(n)O(nlog(n))O(n^2)O(log(n))Question #23What is the time complexity of this function / algorithm?void f(unsigned int n){ int i; for (i = 1; i < n; i = i * 2) { printf("[%d]\n", i); }}O(n!)O(2^n)O(1)O(n)O(nlog(n))O(n^2)O(log(n))Question #24What is the time complexity of searching for an element in a singly linked list of size n?O(n!)O(2^n)O(1)O(n)O(nlog(n))O(n^2)O(log(n))Question #25What is the time complexity of searching for an element in a queue of size n if you are given a pointer to both the head and the tail of the queue?O(n!)O(2^n)O(1)O(n)O(nlog(n))O(n^2)O(log(n))Question #26What is the time complexity of accessing the nth element of a singly linked list?O(n!)O(2^n)O(1)O(n)O(nlog(n))O(n^2)O(log(n))Question #27What is the time complexity of this function / algorithm?void f(int n){ int i; for (i = 0; i < n; i++) { printf("[%d]\n", i); }}O(n!)O(2^n)O(1)O(n)O(nlog(n))O(n^2)O(log(n))Question #28What is the time complexity of this function / algorithm?foreach($numbers as $number){ echo $number;}O(n!)O(2^n)O(1)O(n)O(nlog(n))O(n^2)O(log(n))Question #29What is the time complexity of searching for an element in a doubly linked list of size n?O(n!)O(2^n)O(1)O(n)O(nlog(n))O(n^2)O(log(n))Question #30What is the time complexity of searching for an element - worst case - in a hash table with the implementation you used during the previous Hash Table C project (chaining)?O(n!)O(2^n)O(1)O(n)O(nlog(n))O(n^2)O(log(n))Question #31What is the time complexity of this function / algorithm?int Fibonacci(int number){ if (number <= 1) return number; return Fibonacci(number - 2) + Fibonacci(number - 1);}O(n!)O(2^n)O(1)O(n)O(nlog(n))O(n^2)O(log(n))Question #32What is the time complexity of searching for an element in an unsorted Python 3 list of size n?O(n!)O(2^n)O(1)O(n)O(nlog(n))O(n^2)O(log(n))Question #33What is the time complexity of the “pop” operation onto a stack?O(n!)O(2^n)O(1)O(n)O(nlog(n))O(n^2)O(log(n))Question #34What is the time complexity of inserting after the nth element of a singly linked list? (Assuming you have a pointer to the node to insert)O(n!)O(2^n)O(1)O(n)O(nlog(n))O(n^2)O(log(n))Question #35What is the time complexity of this function / algorithm?void f(unsigned int n){ int i; int j; for (i = 0; i < n; i++) { for (j = 1; j < n; j = j * 2) { printf("[%d] [%d]\n", i, j); } }}O(n!)O(2^n)O(1)O(n)O(nlog(n))O(n^2)O(log(n))Question #36What is the time complexity of searching for an element in an unsorted array of size n?O(n!)O(2^n)O(1)O(n)O(nlog(n))O(n^2)O(log(n))Question #37What is the best case time complexity of insertion in a hash table with the implementation you used during the previous Hash Table C project (chaining)?O(n!)O(2^n)O(1)O(n)O(nlog(n))O(n^2)O(log(n))Question #38What is the best case time complexity searching for an element in a hash table with the implementation you used during the previous Hash Table C project (chaining)?O(n!)O(2^n)O(1)O(n)O(nlog(n))O(n^2)O(log(n))Question #39What is the time complexity of the “push” operation onto a stack?O(n!)O(2^n)O(1)O(n)O(nlog(n))O(n^2)O(log(n))Question #40What is the time complexity of this function / algorithm?def func(n): a=5 b=6 c=10 for i in range(n): for j in range(n): x = i * i y = j * j z = i * j for k in range(n): w = a*k + 45 v = b*b d = 33O(n!)O(2^n)O(1)O(n)O(nlog(n))O(n^2)O(log(n))Question #41What is the time complexity of removing at index n in an unsorted array?O(n!)O(2^n)O(1)O(n)O(nlog(n))O(n^2)O(log(n))Question #42What is the time complexity of inserting into an unsorted Python 3 list at index n?O(n!)O(2^n)O(1)O(n)O(nlog(n))O(n^2)O(log(n))Submit answers
Question #12What is the time complexity of this function / algorithm?def func(n): a=5 b=6 c=10 for i in range(n): for j in range(n): x = i * i y = j * j z = i * j for k in range(n): w = a*k + 45 v = b*b d = 33O(n)O(1)O(log(n))O(nlog(n))O(2^n)O(n!)O(n^2)
If an algorithm has a time complexity of Θ(n2), which of the following is TRUE
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.