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)

Question

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)
🧐 Not the exact question you are looking for?Go ask a 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 at each iteration. This means that the loop will run until 'i' is greater than or equal to 'n'.

If you consider 'n' as 2^m for some m, the loop will run m times. Since m = log(n) (base 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 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)

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

What is the best-case time complexity of Merge Sort?Group of answer choicesO(log n)O(n^2)O(n log n)O(n)

What is the time complexity of searching for an element in a singly linked list?Group of answer choicesO(1)O(n log n)O(n)O(log n)

What is the average time complexity for searching an element in a well-distributed hash table?Group of answer choicesO(n^2)O(1)O(log n)O(n)

1/3