'data structure time complexity solve problem
I want to make sure that it's the right answer. answer :
T(n) = (n log n +1 )
big 0 = O(n log n)
sum = 0; ---- 1
for( i=1; i<=n ; i*=2) ---- log n ?
for(j=1; j<=n ; j++) --- n
sum++; ----1
Solution 1:[1]
The number of iterations of the outer loop is the index of the most significant bit in
n. So, it is indeed floor(log2(n))The number of iterations of the inner loop is exactly n.
Then, the total number of iterations of the body of the inner loop is n * floor(log2(n)), the test, increment and body all execute in constant time.
Hence, the time complexity of this code is O(n.log(n))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
