Skip to content

Time Complexity Analysis of Loop Variations in Code

Time Complexity Analysis of Loops

Loop with Multiplying Counter (Logarithmic)

When a loop's counter variable is multiplied by a constant each iteration, it executes logarithmically.

Example: I = I * 2

for (i = 1; i < n; i = i * 2) {
    // statement
}

Analysis:

  • I starts at 1, then becomes: 1, 2, 4, 8, 16, ... (powers of 2)
  • After k iterations: I = 2^k
  • Loop terminates when I ≥ n, so 2^k = n
  • Therefore, k = log₂ n

Time Complexity: O(log n)

Handling Non-Exact Powers

  • If n = 8: log₂ 8 = 3 (exact), loop runs 3 times
  • If n = 10: log₂ 10 ≈ 3.32, loop runs 4 times (ceiling of log)
  • Always take the ceiling (round up) when dealing with non-exact powers

General Formula for Multiplication

  • Multiplication by 2: O(log₂ n)
  • Multiplication by 3: O(log₃ n)
  • All log bases are O(log n) in Big-O notation

Loop with Dividing Counter (Logarithmic)

When a loop starts at n and divides by a constant each iteration:

Example: I = I / 2

for (i = n; i >= 1; i = i / 2) {
    // statement
}

Analysis:

  • I starts at n, then becomes: n, n/2, n/4, n/8, ...
  • After k iterations: I = n / 2^k
  • Loop terminates when I < 1, so n / 2^k = 1
  • Therefore, 2^k = n, k = log₂ n

Time Complexity: O(log n)

Loop with Incrementing Counter (Linear)

Standard Increment (I++)

for (i = 1; i <= n; i++) {
    // statement
}

Analysis:

  • After k iterations: i = k
  • Loop terminates when i > n, so k = n

Time Complexity: O(n)

Increment by Constant

  • i = i + 2: O(n)
  • i = i + 10: O(n)
  • Any constant increment/decrement is O(n)

Loop with Square Condition (Square Root)

Example: I * I <= N

for (i = 1; i * i <= n; i++) {
    // statement
}

Analysis:

  • Loop runs while i² ≤ n
  • Terminates when i² = n
  • Therefore, i = √n

Time Complexity: O(√n)

Independent Loops (Additive)

Two Independent Linear Loops

for (i = 0; i < n; i++) { /* O(n) */ }
for (j = 0; j < n; j++) { /* O(n) */ }

Analysis:

  • Total operations: n + n = 2n

Time Complexity: O(n) (not O(n²) since loops are not nested)

Dependent Independent Loops

p = 0
for (i = 1; i < n; i = i * 2) { p++ }  // O(log n)
for (j = 1; j < p; j = j * 2) { /* statement */ }  // O(log p)

Analysis:

  • First loop: p takes value log n
  • Second loop: repeats log p times = log(log n) times

Time Complexity: O(log log n)

Nested Loops (Multiplicative)

Linear Outer × Logarithmic Inner

for (i = 0; i < n; i++) {          // O(n)
    for (j = 1; j < n; j = j * 2) {  // O(log n)
        // statement
    }
}

Analysis:

  • Outer loop: n iterations
  • Inner loop: log n iterations per outer iteration
  • Total: n · log n

Time Complexity: O(n log n)

Summary of Loop Time Complexities

| Loop Pattern | Time Complexity | |-------------|-----------------| | i++ (increment by 1) | O(n) | | i += constant | O(n) | | i-- (decrement by 1) | O(n) | | i -= constant | O(n) | | i = i * 2 | O(log₂ n) | | i = i * 3 | O(log₃ n) | | i = i / 2 | O(log₂ n) | | i * i ≤ n | O(√n) |

Key Takeaways:

  • Multiplying or dividing the counter → Logarithmic time
  • Incrementing or decrementing by constant → Linear time
  • Nested loops multiply complexities
  • Independent loops add complexities
  • Ceiling of log is used for non-exact powers

For a deeper understanding of how these loop patterns fit into the broader picture of algorithmic analysis, explore our guide on How to Write and Analyze Algorithms: Time and Space Criteria. To master the foundational concepts behind these complexities, read about Understanding Time and Space Complexity: A Comprehensive Guide to Big O Notation. If you want to practice analyzing nested loops like the multiplicative pattern above, see How to Analyze Time Complexity of Nested Loops and Sequences. For a systematic method to compute these complexities, refer to Algorithm Complexity: Frequency Count Method for Time Space Analysis. Finally, to build your intuition from the ground up, check out Time & Space Complexity Basics: Big O Notation Explained for DSA.

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 LunaNotes

Or 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.”
Bhai Chrome Web Store review

This summary and transcript were automatically generated using AI with the Free YouTube transcript summary tool by LunaNotes.

Related summaries

How to Analyze Time Complexity of Nested Loops and Sequences

How to Analyze Time Complexity of Nested Loops and Sequences

This video teaches you how to calculate the time complexity of various loop structures in code, including simple loops, nested loops, and loops with non-standard increments or conditions. Through detailed tracing and analysis, you'll learn to identify Big O notation for O(n), O(n²), and O(√n) complexities, making algorithm analysis straightforward.

Understanding Time and Space Complexity: A Comprehensive Guide to Big O Notation

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.

Algorithm Complexity: Frequency Count Method for Time Space Analysis

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.

Asymptotic Analysis Part 3: Growth Rates & Time Complexity

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

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.

Found this summary useful?

Take it with you. One click puts it in your own LunaNotes library.

Save to LunaNotes

Start taking better notes today with LunaNotes