An integer n
is strictly palindromic if, for every base b
between 2
and n - 2
(inclusive), the string representation of the integer n
in base b
is palindromic.
Given an integer n
, return true
if n
is strictly palindromic and false
otherwise.
A string is palindromic if it reads the same forward and backward.
Example 1:
Input: n = 9 Output: false Explanation: In base 2: 9 = 1001 (base 2), which is palindromic. In base 3: 9 = 100 (base 3), which is not palindromic. Therefore, 9 is not strictly palindromic so we return false. Note that in bases 4, 5, 6, and 7, n = 9 is also not palindromic.
Example 2:
Input: n = 4 Output: false Explanation: We only consider base 2: 4 = 100 (base 2), which is not palindromic. Therefore, we return false.
Constraints:
4 <= n <= 105
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for this problem involves checking every possible number base to see if the given number is a palindrome in that base. It's like trying out every possible number system to see if the number reads the same forwards and backwards. We start from base 2 and go up to the number itself minus 2.
Here's how the algorithm would work step-by-step:
def is_strictly_palindromic(number):
# Iterate through all bases from 2 to n - 2.
for number_base in range(2, number - 1):
# Convert the number to the current base.
converted_number = convert_to_base(number, number_base)
# Check if the converted number is not a palindrome.
if not is_palindrome(converted_number):
# If not a palindrome, it's not strictly palindromic.
return False
return True
def convert_to_base(number, base):
if number == 0:
return [0]
digits = []
while number:
digits.append(int(number % base))
number //= base
return digits[::-1]
def is_palindrome(digits):
left = 0
right = len(digits) - 1
# Compare digits from the start and end.
while left < right:
if digits[left] != digits[right]:
return False
left += 1
right -= 1
return True
A number is strictly palindromic if it's a palindrome when represented in every base from 2 to the number minus 2. The key insight is realizing that almost no number can possibly meet this condition. We use this understanding to avoid unnecessary calculations and quickly determine the answer.
Here's how the algorithm would work step-by-step:
def is_strictly_palindromic(number) -> bool:
# Numbers <= 3 cannot meet the input constraint.
if number <= 3:
return False
# Numbers > 3 are not strictly palindromic.
# This is based on the fact that any number > 3 will be 12 in base n-2.
return False
Case | How to Handle |
---|---|
Input is a negative number | Return false because negative numbers cannot be strictly palindromic across bases. |
Input is 0 or 1 | Return false since the base-n representation for n > number will always be the number itself, which isn't palindromic by the definition given in the problem. |
Integer overflow during base conversion for large inputs | Check if the base conversion result exceeds the maximum allowed integer value and return false if it does, effectively limiting the tested bases or input number to prevent overflow. |
Input is a prime number | Prime numbers will likely require a base close to the number itself to potentially form a palindrome, requiring efficient base conversion. |
Input is a power of 2 (e.g., 8, 16, 32) | Powers of 2 might have simple binary representations, but it is still unlikely that they will be palindromic in ALL bases from 2 to n-2. |
Large input number, leading to significant computation time during base conversions | Optimize base conversion to be as efficient as possible (e.g., using bitwise operations where feasible) and consider early termination if a non-palindromic base is found. |
Number is close to the maximum integer value | Handle integer overflows carefully when calculating remainders or quotients during base conversion to avoid incorrect results. |
Input number is already a palindrome in base 10 | The solution must still perform the base conversions and palindrome checks for bases 2 through n-2, even if the number is a palindrome in base 10. |