451,985 views
0 votes
0 votes
Calculate the time complexity for the following function in terms of Big O notation. Explain your answer.

[Note: Line numbers are only for reference purpose]

int fun(int n)
{
1 int count = 0;
2 for (int i = n; i >= 0; i /= 2)
3 count += i;
4 for (int j = 0; j < n; j++)
5 count +=j;
6 return count;
}

Calculate the time complexity for the following function in terms of Big O notation-example-1
User Erik Stens
by
3.1k points

1 Answer

23 votes
23 votes

Answer: It is 1 int count = 0;

2 for (int i = n; i >= 0; i /= 2)

3 count += i;

4 for (int j = 0; j < n; j++)

5 count +=j;

6 return count;

SO it is j(0-(-n)

Explanation: Im dum

User Goran Radulovic
by
2.5k points