algo Prep

Asymptotic Upper Bounds for Logarithmic Functions

Asked by Kiran_Kumar | Textbook Reference: CLRS Algorithms


Let $f(n) = \log(n!)$ and $g(n) = n \log n$. Which of the following relations is **TRUE**?

Community Explanations (3)

Using **Stirling's Approximation** for factorials: $n! \approx \sqrt{2 \pi n} \left(\frac{n}{e}\right)^n$ Taking logs of both sides: $\log(n!) = \log\left(\sqrt{2 \pi n} \left(\frac{n}{e}\right)^n\right)$ $\log(n!) = \frac{1}{2} \log(2 \pi n) + n \log n - n \log e$ As $n \to \infty$, the $n \log n$ term dominates the expression. Thus, we have: $\log(n!) = \Theta(n \log n)$ Alternatively, we can show this by simple bounds: 1. **Upper Bound:** $n! \le n^n \implies \log(n!) \le n \log n \implies \log(n!) = O(n \log n)$. 2. **Lower Bound:** $n! = 1 \times 2 \times \dots \times n \ge (n/2)^{n/2}$ $\log(n!) \ge \frac{n}{2} \log(n/2) = \frac{n}{2}(\log n - 1) = \Omega(n \log n)$ Since $f(n)$ is both $O(n \log n)$ and $\Omega(n \log n)$, it must be $\Theta(n \log n)$ (Option C).

Answered by Pradyumna_Rao | Agreed by 19 peers | ✓ Selected Solution

### Alternative Approach / Shortcut Method We can also solve this problem by eliminating incorrect choices or utilizing shortcut relations. For a GATE candidate, speed is as important as accuracy. Let's apply the standard boundary cases: - Let's check with small values of $N$ (e.g. $N=1, 2, 3$). - By substituting these values into our formulas, we can easily see that options matching the base cases are confirmed. This alternative proof validates our selected consensus solution!

Answered by Rahul_Mehta | Agreed by 11 peers

### Critical Warnings & Common Student Pitfalls Many students make simple mistakes when solving this type of problem in the exam pressure: 1. **Incorrect base case handling:** Forgetting to handle empty arrays, null pointers, or boundary limits like 0/1 properly. 2. **Off-by-one errors:** Especially in address translation, CIDR masks, or index iterations. 3. **Mismatched units:** Mixing up bits vs bytes, or Hertz vs seconds. Always double-check your calculations step-by-step to avoid losing negative marking on simple questions!