Derive the recurrence from scratch. WHAT is n!? It's n times the product of everything below it:
n!=n×=(n−1)!(n−1)(n−2)⋯1=n⋅(n−1)!
So n!=n⋅(n−1)! — that's the recursive case. WHY base case 0!=1? An empty product is 1 (multiplying by nothing changes nothing), and it stops the chain.
def factorial(n): if n == 0: # base case return 1 return n * factorial(n - 1) # recursive case: trust the smaller answer
Derive the time bound. If T(n) is the work for n elements: T(n)=T(n/2)+c (one comparison, then half the problem). Unrolling: T(n)=c⋅(log2n)+T(1)=Θ(log2n).
def binary_search(arr, target, lo=0, hi=None): if hi is None: hi = len(arr) - 1 if lo > hi: # base case: empty range -> not found return -1 mid = (lo + hi) // 2 if arr[mid] == target: # base case: found it return mid elif arr[mid] < target: return binary_search(arr, target, mid + 1, hi) # search right half else: return binary_search(arr, target, lo, mid - 1) # search left half
Recall Feynman: explain to a 12-year-old
Imagine you have to find a word in a giant dictionary. You open the middle: if your word comes earlier, you rip away the second half and only look in the first; if later, rip away the first half. Keep ripping in half — you find it super fast. That's binary search.
Recursion is like a line of people passing a question back: "I don't know 5!, but if the person behind me tells me 4!, I'll just multiply by 5." Each person asks the next, until the last person knows the easy answer (0!=1) and passes it forward.
Fibonacci is "next number = sum of previous two," but if you keep asking the same little questions over and over without writing answers down, you waste tons of time — so jot them down (memoize)!
Recursion ka matlab hai ek function jo apne aap ko hi call karta hai, lekin har baar thode chote problem pe. Do cheezein har recursion mein zaroori hain: base case (sabse chota case jahan seedha answer return ho jaye, taaki chain ruk jaye) aur recursive case (jahan tum chote version pe khud ko call karke result jodte ho). Soch lo jaise ek line mein log khade hain — har banda peeche wale se poochta hai, aur aakhri banda jise easy answer pata hai wo wapas bhej deta hai. Yahi "Base, Break, Believe" wala funda hai.
Factorial simple hai: n!=n×(n−1)!, aur 0!=1. Bas isi rule ko code mein likh do. Fibonacci mein Fn=Fn−1+Fn−2, isliye do base cases chahiye (F0=0,F1=1). Par ek warning: naive Fibonacci bohot slow hai kyunki wo same chote sawaal baar-baar solve karta hai — fib(40) hi atak jaata hai. Iska fix hai memoization, yaani answers ko ek dictionary mein cache kar lo, phir har value sirf ek baar calculate hoti hai.
Binary search sabse pyaara example hai. Condition: array sorted hona chahiye. Tum beech wale (mid) element se compare karte ho — agar target chota hai to left half mein dhoondo, bada hai to right half mein. Har step mein aadha array fenk dete ho, isliye time log2n ho jaata hai. Agar n=10lakh ho, tab bhi sirf ~20 steps! Bas yaad rakho: unsorted array pe binary search galat answer dega — pehle sort karo.