Smallest Good Base - Problem
Given an integer n represented as a string, return the smallest good base of n.
We call k >= 2 a good base of n, if all digits of n base k are 1's.
In other words, n represented in base k should be 111...1 (all ones).
Input & Output
Example 1 — Basic Case
$
Input:
n = "13"
›
Output:
"3"
💡 Note:
13 in base 3 is 111: 1×3² + 1×3¹ + 1×3⁰ = 9 + 3 + 1 = 13. Base 2 gives 1101 (not all 1s), so 3 is the smallest good base.
Example 2 — Small Number
$
Input:
n = "4"
›
Output:
"3"
💡 Note:
4 in base 3 is 11: 1×3¹ + 1×3⁰ = 3 + 1 = 4. Base 2 gives 100 (not all 1s), so 3 is the smallest good base.
Example 3 — Edge Case
$
Input:
n = "3"
›
Output:
"2"
💡 Note:
3 in base 2 is 11: 1×2¹ + 1×2⁰ = 2 + 1 = 3. This is the smallest base where 3 consists of all 1's.
Constraints
- n is represented as a string
- 3 ≤ n ≤ 1018
- n does not have leading zeros
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code