Level 1: The Land of Big-O

0/3 Games Completed

๐Ÿ“š What is Big-O Notation?

๐ŸŽฏ The Core Idea

Big-O notation describes how an algorithm's performance scales as input size grows.

Input Size (n)
10
O(1) 1
O(log n) 3
O(n) 10
O(n log n) 33
O(nยฒ) 100

โš ๏ธ Key Rules

  • 1๏ธโƒฃ
    Count Iterations, NOT Time

    Execution time varies by machine. Iterations are consistent.

  • 2๏ธโƒฃ
    Ignore Constants

    O(2n) โ†’ O(n), O(100) โ†’ O(1)

  • 3๏ธโƒฃ
    Keep Only the Highest Order Term

    O(nยฒ + n + 100) โ†’ O(nยฒ)

Level 2: Why Count Iterations?

0/1 Games Completed

๐Ÿ“š Iterations vs Execution Time

๐Ÿค” Why NOT measure execution time?

๐Ÿ’ป
Machine

A supercomputer runs faster than a laptop

๐Ÿ–ฅ๏ธ
OS

Windows, Linux, macOS have different overheads

๐Ÿ“
Language

C++ runs faster than Python

๐Ÿ”ง
Compiler

Different optimizations affect speed

โœ… Why COUNT iterations?

Iterations remain CONSTANT regardless of:

  • Machine speed
  • Operating system
  • Programming language
  • Compiler optimizations
๐Ÿ’ก

Iterations measure the algorithm's efficiency, not the system's speed!

Level 3: Asymptotic Kingdom

0/1 Games Completed

๐Ÿ“š Asymptotic Notation

O (Big-O)

Upper Bound

"At most this many operations"

โญ Most commonly used

ฮ˜ (Theta)

Tight Bound

"Exactly this order of growth"

ฮฉ (Omega)

Lower Bound

"At least this many operations"

๐Ÿ† Why Big-O for Competitions?

In coding competitions, we care about the worst case because:

  • Test cases include maximum input sizes (N up to 10โถ or more)
  • We need to ensure our solution doesn't exceed time limits
  • Big-O gives us the upper bound on operations

Level 4: Lower Term Desert

0/1 Games Completed

๐Ÿ“š Why Discard Lower Order Terms?

๐ŸŽ The Apple vs Orange Analogy

๐ŸŽ

1,000,000 apples

+
๐ŸŠ

10 oranges

=

โ‰ˆ 1,000,000 apples

The oranges are negligible!

๐Ÿ“Š Mathematical Proof

n n nยฒ nยณ Ratio n:nยฒ
10 10 100 1,000 10%
100 100 10,000 1,000,000 1%
1000 1,000 1,000,000 1,000,000,000 0.1%
๐Ÿ’ก

As n grows, lower terms become negligible!

Level 5: Constant Coefficient Battle

0/1 Games Completed

๐Ÿ“š Why Discard Constant Coefficients?

๐Ÿ“Š Comparing 5n, 2n, and 6n

When n = 100:

5n = 500
2n = 200
6n = 600
๐Ÿ’ก

All three are O(n) โ€” they all grow linearly!

๐ŸŽฏ The Key Insight

Big-O describes the shape of growth, not the exact count.

  • 5n grows linearly โ†’ O(n)
  • 100n grows linearly โ†’ O(n)
  • 0.001n grows linearly โ†’ O(n)

Constants only affect the speed, not the scalability.

Level 6: Threshold Value Valley

0/1 Games Completed

๐Ÿ“š The Crossover Point

โš ๏ธ Sometimes O(nยฒ) beats O(n)... for small N!

Consider two algorithms:

Algorithm 1

Time = 10n

O(n)

VS
Algorithm 2

Time = nยฒ

O(nยฒ)

๐Ÿ“Š Finding the Crossover

n 10n nยฒ Winner
5 50 25 nยฒ wins!
10 100 100 Tie!
20 200 400 10n wins!
100 1,000 10,000 10n wins!
๐Ÿ’ก

Crossover point: n = 10. Beyond this, O(n) always wins!

Level 7: Worst vs Best Case Depths

0/1 Games Completed

๐Ÿ“š Best Case vs Worst Case

๐Ÿ“ Linear Search Example

for (int i = 0; i < n; i++) {
    if (arr[i] == target) {
        return true;  // Found!
    }
}
return false;  // Not found

๐ŸŒŸ Best Case

Target at index 0

O(1)

Found immediately!

๐Ÿ“Š Average Case

Target in middle

O(n/2) = O(n)

Check half the array

๐Ÿ’€ Worst Case

Target at last index or not found

O(n)

Check entire array!

๐Ÿ’ก

We typically use worst case in Big-O to guarantee performance!

Level 8: Space Complexity Forest

0/1 Games Completed

๐Ÿ“š Understanding Space Complexity

๐Ÿ’พ Memory Usage of Data Types

int 4 bytes
long 8 bytes
float 4 bytes
double 8 bytes
char 1 byte
int[n] n ร— 4 bytes

๐Ÿ“Š Example Calculation

int n;              // 4 bytes
int arr[n];         // n ร— 4 bytes
int sum = 0;        // 4 bytes

Total = 4 + 4n + 4 = 4n + 8 bytes
Space Complexity = O(n)

Level 9: TLE Volcano

0/1 Games Completed

๐Ÿ“š Time Limit Exceeded (TLE)

๐ŸŒ‹ What Causes TLE?

  • Too many operations: 10โธ - 10โน is often the limit
  • Infinite loops: Loop never terminates
  • Inefficient algorithm: O(nยฒ) when O(n log n) is needed

๐Ÿ“Š Operations Per Second

A typical judge can execute ~10โธ operations/second

Operations Time Result
10โถ 0.01s โœ… Safe
10โท 0.1s โœ… Safe
10โธ 1s โš ๏ธ Borderline
10โน 10s โŒ TLE!
10ยนโฐ 100s ๐ŸŒ‹ TLE!

Level 10: Integer Overflow Dungeon

0/1 Games Completed

๐Ÿ“š Integer Overflow

๐Ÿ’ฅ What is Integer Overflow?

When a number exceeds the maximum value a data type can hold, it "wraps around" to unexpected values!

int (32-bit) Max: 2,147,483,647 (~2ร—10โน)
long (64-bit) Max: 9,223,372,036,854,775,807 (~9ร—10ยนโธ)

โš ๏ธ Dangerous Example

int a = 1000000000;  // 10โน
int b = 1000000000;  // 10โน
int c = a * b;       // 10ยนโธ - OVERFLOW!

// Result: c = garbage value!
// Expected: 1000000000000000000
// Actual: Some random negative number!

โœ… The Fix

long a = 1000000000L;
long b = 1000000000L;
long c = a * b;  // Works correctly!

// Or cast before multiplication:
int a = 1000000000;
int b = 1000000000;
long c = (long)a * b;  // Also works!
๐Ÿ†

Congratulations!

You've Mastered Big-O Notation!

Key Concepts Learned:

  • โœ… What Big-O notation represents
  • โœ… Why we count iterations, not time
  • โœ… Asymptotic notation (O, ฮ˜, ฮฉ)
  • โœ… Why discard lower order terms
  • โœ… Why discard constant coefficients
  • โœ… Threshold values and crossover points
  • โœ… Best case vs worst case analysis
  • โœ… Space complexity calculation
  • โœ… Time Limit Exceeded prevention
  • โœ… Integer overflow handling