Largest Fancy Number At Most N

Combinatorics · Hard · Free problem
Call a positive integer *fancy* if every digit in its base-4 representation is either 0 or 1 (so 0, 1, 4, 5, 16, 17, 20, 21, ... are fancy in decimal). Given an integer $N$ with
\leq N \leq 10^{18}$, return the largest fancy number that is $\leq N$. Your solution must run in $O(\log N)$ time. **Examples:** - $N = 6$: Base-4 digits of 6 are `12`. The largest fancy number $\leq 6$ is 5 (base-4: `11`). Output: `5` - $N = 1$: Already fancy. Output: `1` - $N = 100$: Base-4 is `1210`. Largest fancy $\leq 100$ is 85 (base-4: `1111`). Output: `85`

Open the full interactive solver, hints, and worked solution →