What is the time complexity of the following code fragment?for(i=1; i<n; i= i*2) a[i]=0;
Question
Solution 1
The time complexity of the given code fragment is O(log n).
Here's why:
The loop variable 'i' is being doubled in each iteration (i = i*2). This means that the loop will run as long as 'i' is less than 'n'.
In terms of iterations, the loop will run for the number of times you can repeatedly div 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
Similar Questions
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 the following code fragment?for(i=1; i<n; i= i*2) a[i]=0; Group of answer choicesO(n/2)O(n^(1/2))O(n^2)O(log n)
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 : int a = 0, i = N; while (i > 0) { a += i; i /= 2; }
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; }}