Five problems where a function calls itself more than once: the largest number by halves, the Tower of Hanoi, counting ways to climb, binary strings under a rule, and lucky numbers.
- Write the base cases first — and check whether you need two (done and went too far).
- For the maximum, split
[l, r]in two and take the bigger of the two answers. - The Tower of Hanoi prints up to a million moves. Print the count first —
2ⁿ − 1, found by doubling 1
ntimes and taking away 1 — and use"\n"with the fast-IO lines. - For the binary strings, try
0before1and the order comes out right by itself. - For lucky numbers, build every number made of 4s and 7s up to
n, and check each one.