#483

Smallest Good Base

master · 1610 · lc hard +32 · verified · 45.6% accepted · 426 likes · top 29%

play →

Description

Call k >= 2 a good base of integer n when the base-k representation of n consists entirely of 1s.

Given n encoded as a string, return the smallest such good base as a string.

Example 1:

Input: n = "13"
Output: "3"
Explanation: 13 base 3 is 111.

Example 2:

Input: n = "4681"
Output: "8"
Explanation: 4681 base 8 is 11111.

Example 3:

Input: n = "1000000000000000000"
Output: "999999999999999999"
Explanation: 1000000000000000000 base 999999999999999999 is 11.

Code

1
2
3