1 2 3 4 |
|
What is the time complexity of the above code snippet shown above?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
For each i in the first
for
loop, the secondfor
loop will runcount = count + 1
i times. Since i ranges from 0 to n . The number of iterations are1 + 2 + ⋯ + n = 2 n ( n + 1 ) → O ( n 2 )
Rule of thumb : take only the one with the highest power, since they contribute more to the runtime, and remove any constants and coefficients.