What is time complexity of fun()?
int fun(int n) {
int count = 0;
for (int i = n; i > 0; i /= 2)
for (int j = 0; j < i; j++)
count += 1;
return count;
}
int fun(int n)
{
int count = 0;
for (int i = n; i > 0; i /= 2)
for (int j = 0; j < i; j++)
count += 1;
return count;
}
int fun(int n) {
int count = 0;
for (int i = n; i > 0; i /= 2)
for (int j = 0; j < i; j++)
count += 1;
return count;
}
def fun(n):
count = 0
i = n
while i > 0:
for j in range(i):
count += 1
i //= 2
return count
int Fun(int n) {
int count = 0;
for (int i = n; i > 0; i /= 2)
for (int j = 0; j < i; j++)
count += 1;
return count;
}
function fun(n) {
let count = 0;
for (let i = n; i > 0; i = Math.floor(i / 2))
for (let j = 0; j < i; j++)
count += 1;
return count;
}
O(n2)
O(n*log(n))
O(n)
O(n*log(n*Log(n)))
This question is part of this quiz :
Top MCQs on Complexity Analysis of Algorithms with Answers