Simple Loop Analysis (O(n))
Basic Incrementing Loop
for i in range(n): # i from 0 to n-1
statement
- The loop runs n times.
- The statement executes exactly n times.
- Time complexity: O(n).
Decrementing Loop
i = n
while i > 0:
statement
i -= 1
- Goes from n down to 1 → still n steps.
- Whether you go up (1 to 10) or down (10 to 1), the number of iterations is the same.
- Time complexity: O(n).
Loop with Step Size (c)
for i in range(1, n, 2): # step size 2
statement
- If incrementing by 2, loop runs about n/2 times → still O(n).
- If incrementing by 20, it runs about n/20 times → still O(n).
- Key rule: A constant divisor or multiplier does not change the polynomial degree; complexity remains O(n).
Nested Loops (O(n²))
Standard Nested Loop (i and j both range over n)
for i in range(n):
for j in range(n):
statement
- Inner loop runs n times for each of the n outer iterations.
- Total executions = n · n = n².
- Time complexity: O(n²).
Nested Loop with j Dependent on i
for i in range(n): # i from 0 to n-1
for j in range(0, i): # j from 0 to i-1
statement
Tracing the execution:
- When i = 0: j runs 0 times.
- When i = 1: j runs 1 time.
- When i = 2: j runs 2 times.
- ...
- When i = n-1: j runs n-1 times.
Total iterations: 0 + 1 + 2 + ... + (n-1) = n(n-1)/2
The highest degree term is n²/2, so:
- Time complexity: O(n²).
- Note: Even though the inner loop runs fewer times on average, the degree of the polynomial is still n².
Loop with Changing Variable (O(√n))
p = 0
for i in range(1, n):
if p <= n:
p += i
else:
break
- Here, p accumulates the sum of the first k integers: 1 + 2 + ... + k = k(k+1)/2.
- The loop stops when p > n.
- Condition: k(k+1)/2 > n.
- Approximate: k² > n, so k ≈ √n.
- Time complexity: O(√n).
Example:
- If n = 100, p becomes 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105 → stops at 105 > 100, running about 14 times ≈ √100 = 10. The actual number is close to √n.
- This shows that not all loops run n times; you must trace the variable's growth.
Key Takeaways
| Loop Type | Example | Number of Iterations | Complexity |
|---|---|---|---|
| Simple increment | for i in range(n) | n | O(n) |
| Decrement | i = n; while i > 0: i-- | n | O(n) |
| Step constant | for i in range(0, n, k) | n/k | O(n) |
| Standard nested | for i in range(n)... for j in range(n) | n² | O(n²) |
| Dependent inner | for i in range(n)... for j in range(i) | n(n-1)/2 | O(n²) |
| Accumulating p | p += i until p > n | ≈ √n | O(√n) |
Always analyze the growth of the variable controlling the loop, not just a quick glance, especially when the increment or condition is non-standard. For a deeper understanding of how these patterns translate to formal notation, see the section on Understanding Time and Space Complexity: A Comprehensive Guide to Big O Notation and additional methods like the Algorithm Complexity: Frequency Count Method for Time Space Analysis. To strengthen your foundational comprehension, check out Time & Space Complexity Basics: Big O Notation Explained for DSA and How to Write and Analyze Algorithms: Time and Space Criteria.
welcome that I relate few pieces of code and show you how to analyze them how to find out the time complexity for them
this is a simple loop I am not reading a complete algorithm I have this written a piece of code in which one statement is
there but we are already familiar with this one we know that this will execute for n plus 1 9 and this way is good for
and we don't want this time because anyway when we are at the order of be writing a degree so this will not affect
the degree at all so it will be same only so just you need to move the statement how many times it is executed
so that's it so this is order of n you can avoid this one skip this one next if suppose this loop starting from N and
the condition is greater than 1 or 0 and this was getting subtracted so this loop is decrementing then also this statement
will execute for n times and it sort of in whether you go from one to ten ten steps or come from ten to one then also
ten steps so you go from one to ten or ten to one signal process so that's how n to 0 at us so it's also all wrong and
if this is starting from one and condition is less than N and I is being incremented by 2 so this statement will
not repeat for ten times but half of the end times and by two if it is n by 2 also if I say function s and by 2 what
is the degree of polynomial and only search part of n again so in this case also it sort of end even
if it is incrementing by 20 then this is 20 degree of a polynomial is and so resolves forever friend so end by
anything this whatever the number of times it is implementing the time will be same
nice we are already familiar with this one these are two loops nested loop one inside another and this we know this
relates q4 n times so this is also n times and resolves right times actually is n plus 1 but what about this one
again this is repeat for n plus 1 times and so they before any time and this is all rough and squared
the next is again nested for loop this looks similar to the previous one but not see I did travel 0 I less than n but
G takes value G less than I so here is a defense so let us analyze this by tracing this one and find out how much
time it is checking so for this I will take I value and J value and paste them let us start initially I is 0 when I 0 J
starts from 0 and 0 less than 0 no it will not execute for any number of times so here I have to find out how
many times the statement is executed so number of times so it will not execute at all so 0 times then this loop ends
again I am plus plus I becomes 1 I is less than n in suppose it's enters inside then again this group 0 0 is less
than 9 yes then J plus plus J is 1 1 is not less than I so for 1 it will not execute so how many times it has
executed one time and then goes up again I plus plus I becomes 2 then J will execute for 0 1 and when it becomes 2 it
will stop so this will be 4 2 times and when I is the 3 J will take 0 1 2 & 3 so this will be 4 3 times and this process
continues see when I was 0 it was never executed when I was 1 it was executed for one
time this is statement I was true it was a spirit for two time three rate and so on so on up to what
and so total when it is n right how many times this will execute n times when I was three so three times so if it is
reaching till n actually it is less than n but let us say m is reaching till 10 so it will execute for 10 times so total
how many times it is executed this one right so I am assuming that it is going till then no it is not didn't this less
than n times 1 plus 2 Plus 3 plus goes on to and this is nothing but n into n person
by 2 what is the degree of this one F of n as n square plus 1 by 2 Davy's square so all Roth and square for
the time complexity is order of n square though this is not similar to the previous one but if you're analyzed
answer is coming 2 and square only the degree of the polynomial is n square only next for is here
Beasley's rest with you and as a loop does not get very good for n times C the speed P is less than equal to n it's not
a less than equal to n so the condition is different then what is P P is initially 0 and every time I is added to
P so now we don't know how many times it's going to execute let us analyze I will take I value and B
value initially this is 0 and this is 1 so what happens first time this one is added and it becomes 1 the next time it
will be do so it will be 1 plus 2 so it will be 3 then next time I will be 3 and this will be 1 plus 2 Plus 3 then idle
before it will be 1 plus 2 Plus 3 plus 4 goes on how many times it's going to execute it's going to execute for K
times so what this will be 1 plus 2 Plus 3 plus 4 plus goes on to K time my hope K here is not reaching it is not
repeating for n times definitely it is red waiting for some number of times so that we don't know
we are sure that it still not execute for n times this how many times because the conditions
so I said let it be came and let us find out that came so if it is repeating for K times then what is the tradition we
are seeing that P has became greater than M so when P became greater than n it is stopping we assume this as long as
E is less than equal to n it is repeating so when P becomes greater than n it is stopping right since P is what
CP see it is getting to k plus 1 by 2 P is akin to k plus 1 by 2 so what's the condition K mu K plus 1 by 2 is greater
than n so when it will stop when k k plus 1 by 2 has became greater than n it'll stop so what is K roughly as limit
as K square so what is K gains and K is a root n so this loop will execute for order of n times let me show you quickly
I assignment 1 I P is less than equal to n I dismiss this is not I less than equal to n so we don't know how many
times it is repeating II's here P is here so either these are incrementing and every time I is added to P so we
don't know how many times so let us say K times and this will be 1 plus 2 plus 2 K well so that K s P is gain k plus 1 by
2 now so we know that this was repeated as long as P is less than equal to so if you'll stop then P becomes greater
than n so then P becomes greater than n if it stops okay and keep us on white goo becomes greater than it will stop so
this is K Square and K is okay in the next video we'll find more examples right so different type of course I will
show you and I'll show you how to analyze them so this is all by tracing you canonized n so in the other video
next video also you can find few more algorithms that the Tracy
Always trace the growth of the variable controlling the loop rather than relying on quick heuristics. The number of iterations may follow a polynomial, logarithmic, or fractional exponent pattern based on how the variable changes.
The inner statement executes exactly n × n = n² times, as the inner loop runs n times for each of the n outer iterations. This yields O(n²) time complexity.
No, a loop with a constant step size (e.g., step 20) runs n/k times, which is still O(n). The constant divisor does not change the polynomial degree of the time complexity.
Even though the inner loop runs fewer times on average (0 to n-1 times), the total iterations sum to n(n-1)/2. The highest-degree term is n², resulting in O(n²) time complexity.
The loop stops when the accumulated sum p exceeds n. Since p grows quadratically (sum of first k integers is k(k+1)/2), the condition k² > n yields k ≈ √n, resulting in O(√n) complexity.
A loop that runs from 1 to n, whether incrementing or decrementing, executes n iterations, resulting in O(n) time complexity. The direction of the loop does not change the number of steps.
Keep this summary
Save it to LunaNotes and it becomes a real note in your library — editable, searchable, and ready to turn into flashcards or a diagram. Free to start.
Save to LunaNotesOr summarise for another video.
32K+ people have turned 65K+ videos into notes
“Very useful tool. I'm using it for the last couple weeks and I'm loving it so far. Makes learning on YouTube a great experience.”
This summary and transcript were automatically generated using AI with the Free YouTube transcript summary tool by LunaNotes.
Related summaries
Time Complexity Analysis of Loop Variations in Code
Learn to analyze time complexity for different loop structures, including multiplying counters (log n), incrementing counters (n), and nested loops (n log n). This guide covers key formulas and examples to master algorithmic efficiency.
Understanding Time and Space Complexity: A Comprehensive Guide to Big O Notation
Explore the concepts of time complexity, space complexity, and Big O notation with practical examples.
Asymptotic Analysis Part 3: Growth Rates & Time Complexity
Learn why we use asymptotic complexity instead of exact running times in algorithm analysis. This tutorial explains how time complexity depends on input size and why higher-order terms dominate for large inputs, using clear examples with array shifts and growth rate calculations.
Time & Space Complexity Basics: Big O Notation Explained for DSA
This video introduces time complexity and space complexity concepts for coding interviews. The instructor explains why time taken is not time complexity, defines Big O notation, and covers the three key rules for computing complexity: worst-case scenario, avoiding constants, and ignoring lower-order terms. It also covers space complexity, auxiliary vs input space, and the crucial rule of not modifying input data.
Algorithm Complexity: Frequency Count Method for Time Space Analysis
Learn how to analyze algorithm efficiency using the frequency count method. This video demonstrates time and space complexity analysis for sum of array elements, matrix addition, and matrix multiplication algorithms.
Most viewed summaries
A Comprehensive Guide to Using Stable Diffusion Forge UI
Explore the Stable Diffusion Forge UI, customizable settings, models, and more to enhance your image generation experience.
Kolonyalismo at Imperyalismo: Ang Kasaysayan ng Pagsakop sa Pilipinas
Tuklasin ang kasaysayan ng kolonyalismo at imperyalismo sa Pilipinas sa pamamagitan ni Ferdinand Magellan.
Mastering Inpainting with Stable Diffusion: Fix Mistakes and Enhance Your Images
Learn to fix mistakes and enhance images with Stable Diffusion's inpainting features effectively.
Pamamaraan at Patakarang Kolonyal ng mga Espanyol sa Pilipinas
Tuklasin ang mga pamamaraan at patakaran ng mga Espanyol sa Pilipinas, at ang epekto nito sa mga Pilipino.
How to Install and Configure Forge: A New Stable Diffusion Web UI
Learn to install and configure the new Forge web UI for Stable Diffusion, with tips on models and settings.
Found this summary useful?
Take it with you. One click puts it in your own LunaNotes library.
Save to LunaNotes