What 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(1)O(log(n))O(nlog(n))O(2^n)O(n!)O(n^2)
Question
What 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(1)O(log(n))O(nlog(n))O(2^n)O(n!)O(n^2)
Solution
The time complexity of the given function is O(n^2). This is because there are two nested loops in the function. The outer loop runs n times and for each iteration of the outer loop, the inner loop runs n times. Therefore, the total number of operations is proportional to n^2.
Similar Questions
What 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); }}
What is the time complexity of this function / algorithm?void f(int n){ printf("n = %d\n", n);}
What is the time complexity of this function / algorithm?void f(int n){ int i; for (i = 0; i < n; i += 98) { printf("[%d]\n", i); }}
What is the time complexity of the following code? for(int i = 0; i < n; i++) { for(int j = 0; j < i; j++) { System.out.println(i * j); } } a. O(n) b. O(2^n) c. O(n^2) d. O(n log n)
What is the time complexity of the following code :int i, j, k = 0;for (i = n/2; i <= n; i++) { for (j = 2; j <= n; j = j * 2) { k = k + n/2; }}
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.