# Why Your Code Gets TLE (Time Limit Exceeded) 

When solving problems on platforms like **Codeforces** or **LeetCode**, many beginners face a common issue:

**TLE (Time Limit Exceeded)**

Let’s understand this in the simplest way possible.

* * *

### The Hidden Rule: 10⁸ Operations per Second

Most competitive programming platforms assume:

> In **1 second**, your code can perform around **10⁸ operations**

This is not exact, but a **safe assumption**.

* * *

### What Does This Mean?

Case 1:

If you run a loop:

```cpp
for (int i = 0; i < 10^8; i++)
```

It will roughly take **1 second**

Case 2:

If:

```plaintext
n = 10^9
```

and you run:

```plaintext
for (int i = 0; i < n; i++)
```

Total operations = **10⁹**

**Rule:** 1 sec we can do only 10⁸ operation

Time ≈ **10 seconds** (Too slow → TLE)

* * *

### So What’s the Problem?

In many problems (like [**Range Sum Query**](https://codeforces.com/group/4vcXCPx8NY/contest/678202/problem/A)), constraints are:

`q ≤ 10^5`

![](https://cdn.hashnode.com/uploads/covers/624226a5db84f8c50fa5b247/3db1db10-0174-40e3-9a71-1712234ff99e.png align="center")

If you solve each query using a loop:

For each query, we are running a loop from **L to R**.

```javascript
for (int i = 0; i < q; i++) {
	int ans = 0;
	for (int i = l; i <= r; i++) {
	   ans+=arr[i];
	}
}
```

**Worst Case Scenario**

*   Number of queries = `q = 10^5`
    
*   In worst case, each query covers the full array  
    `L = 1, R = n = 10^5`
    

So for **one query**, loop runs:

```plaintext
10^5 times
```

**Total Operation**

Now we do this for **every query**:

```plaintext
Total operations = 10^5 (queries) × 10^5 (loop per query)
                 = 10^10 operations
```

**Convert Operations → Time**

We know:

> 1 second ≈ 10^8 operations

Now divide total work:

```plaintext
10^10 ÷ 10^8 = 100 seconds
```

**Final Result**

*   Your program needs **~100 seconds**
    
*   But time limit is **1 second**
    

So it will **definitely give TLE**

* * *

### Now the Real Question

> At what time complexity should we write our solution?

We already know:

> 1 second ≈ 10⁸ operations

So now the goal is simple:

**Total operations in your code should be ≤ 10⁸**

* * *

## How to Decide Time Complexity from Constraints

We use the value of **N (input size)** to decide what kind of loop we can run.

### Quick Cheat Sheet

| N (Input Size) | Allowed Complexity |
| --- | --- |
| 10² | O(n²), O(n³) |
| 10³ | O(n²) |
| 10⁵ | O(n log n), O(n) |
| 10⁷ | O(n) borderline |
| 10⁹ | O(log n) |
| 10¹⁸ | O(log n) or O(1) |

### Case 1: N = 10⁵

If you write:

```cpp
for (int i = 0; i < n; i++)
```

Operations = `10⁵` → Very fast

If you write:

```cpp
for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
```

Operations = `10⁵ × 10⁵ = 10¹⁰` TLE

* * *

### Case 2: N = 10⁹

```cpp
for (int i = 0; i < n; i++)
```

Operations = `10⁹` → Too slow

So what can we do?

Use **logarithmic solution**

Example:

```cpp
while (n > 0) {
    n = n / 2;
}
```

Operations ≈ `log₂(10⁹) ≈ 30`

* * *

### Case 3: N = 10¹⁸

You **cannot even loop once up to N**

So only allowed:

*   O(1)
    
*   O(log n)
    

Example:

```plaintext
while (n > 0) {
    n /= 2;
}
```

~60 operations only

* * *

### Key Insight

> Bigger the constraint → Smaller the allowed time complexity

* * *

### Example Thinking (How Top Coders Think)

Instead of memorizing formulas, good programmers do **quick mental math** before coding.

Let’s break each case properly

* * *

### Case 1: `n = 10⁵`

Ask yourself:

*   If I run **one loop** → `10⁵` operations (very fast)
    
*   If I run **n log n** →  
    `10⁵ × log₂(10⁵) ≈ 10⁵ × 17 = 1.7 × 10⁶` (safe)
    
*   If I run **nested loop (n²)** →  
    `10⁵ × 10⁵ = 10¹⁰` (too slow)
    

### Conclusion:

> For `n = 10⁵`, you can safely use:

*   O(n)
    
*   O(n log n)
    
*   O(n²)
    

* * *

### Case 2: `n = 10⁹`

Now think:

*   One loop → `10⁹` operations
    
*   But limit is - In 1 sec we can iteration 10⁸ only
    

```plaintext
10⁹ ÷ 10⁸ = 10 seconds 
```

### So what to do?

You **must avoid looping till n**

Instead think:

*   Can I use **math formula**?
    
*   Can I use **binary search**?
    
*   Can I reduce problem using **logarithm**?-
    

* * *

### Golden Rule

> Don’t start coding immediately  
> First check: **Will my solution fit in 10⁸ operations?**

* * *

### Final Takeaway

*   Constraints tell you **how to think**
    
*   Time complexity tells you **what to write**
    
*   Optimization is just **reducing operations**
