Reputation: 1
can any one tell me why is the time complexity O(n) while the outer loop runs for log(n) and inner loop runs for n time the time complexity should be O(n*log(n)) but its O(n)?
Upvotes: 0
Views: 26
Reputation: 4495
The inner loop doesn't run n
times, it runs i
times. i
varies in the outer loop. So to find the time complexity, you set up an expression for how many times the inner loop runs. In your image, this is already done in the explanation.
From there, after some mathematics, you will find that the time complexity is O(n). Hint: Geometric series.
Upvotes: 0