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; }}

Question

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; }}
🧐 Not the exact question you are looking for?Go ask a question

Solution 1

The time complexity of the given code can be calculated as follows:

  1. The outer loop runs from n/2 to n. So, the time complexity of the outer loop is O(n).

  2. The inner loop runs from 2 to n with an increment of j = j * 2. This is a geometric progression, and the time complexity of this loop is O Knowee AI StudyGPT is a powerful AI-powered study tool designed to help you to solve study prob

Knowee AI StudyGPT is a powerful AI-powered study tool designed to help you to solve study problem.
Knowee AI StudyGPT is a powerful AI-powered study tool designed to help you to solve study problem.
Knowee AI StudyGPT is a powerful AI-powered study tool designed to help you to solve study problem.
Knowee AI StudyGPT is a powerful AI-powered study tool designed to help you to solv

This problem has been solved

Similar Questions

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; }}

What is the time complexity of the following code : int a = 0, i = N; while (i > 0) { a += i; i /= 2; }

The time complexity of the  following code snippet isc=0; while (n>=1) {for(i=1;i>=n;i++){ c++;}n=n/2;}

What is the time complexity of the following code fragment?for(i=1; i<n; i= i*2)          a[i]=0;

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); }}

1/3