Strictly Palindromic Number - Problem

A strictly palindromic number is a fascinating mathematical concept that challenges our understanding of number representations across different bases.

Given an integer n, we need to determine if it's strictly palindromic. This means that when we convert n to every possible base from 2 to n-2 (inclusive), the resulting string representation must be a palindrome in all of those bases.

What's a palindrome? A string that reads the same forwards and backwards, like "racecar" or "121".

Example: For n = 9, we'd check bases 2 through 7:
• Base 2: 9 = "1001" ✓ (palindrome)
• Base 3: 9 = "100" ✗ (not palindrome)
Since it fails in base 3, n = 9 is not strictly palindromic.

Goal: Return true if the number is strictly palindromic, false otherwise.

Input & Output

example_1.py — Python
$ Input: n = 9
Output: false
💡 Note: In base 2: 9 = "1001" (palindrome), but in base 3: 9 = "100" (not palindrome). Since it fails in at least one base, return false.
example_2.py — Python
$ Input: n = 4
Output: false
💡 Note: We only check base 2: 4 = "100" (not palindrome). Since the only base to check fails, return false.
example_3.py — Python
$ Input: n = 1
Output: true
💡 Note: For n = 1, we need to check bases 2 to n-2 = -1. Since there are no valid bases to check, we return true by definition.

Visualization

Tap to expand
Mathematical Insight: Why Answer is Always FalseBase (n-1) AnalysisFor n in base (n-1):n = 1×(n-1) + 1 = "11"✓ Always palindromicBase (n-2) AnalysisFor n ≥ 4 in base (n-2):n = 1×(n-2) + 2 = "12"✗ Never palindromicExample: n = 9Base 8 (n-1): 9 = "11"9 ÷ 8 = 1 remainder 1 → "11" ✓Base 7 (n-2): 9 = "12"9 ÷ 7 = 1 remainder 2 → "12" ✗🎯 Key InsightSince base (n-2) always produces a non-palindromic resultfor n ≥ 4, the answer is ALWAYS false!
Understanding the Visualization
1
Base (n-1) Analysis
In base (n-1), number n is always "11" (palindromic)
2
Base (n-2) Analysis
In base (n-2), number n has a specific non-palindromic pattern
3
Mathematical Proof
Since base (n-2) always fails for n ≥ 4, the answer is always false
Key Takeaway
🎯 Key Insight: This is a mathematical brainteaser! For any n ≥ 4, the number will always fail the palindrome test in base (n-2), making the optimal solution simply `return false`.

Time & Space Complexity

Time Complexity
⏱️
O(n² log n)

O(n) bases to check, each base conversion takes O(log n), palindrome check takes O(log n)

n
2n
Quadratic Growth
Space Complexity
O(log n)

Space needed to store the string representation in each base

n
2n
Linearithmic Space

Constraints

  • 4 ≤ n ≤ 2 × 109
  • The answer for all valid inputs will be false
Asked in
Meta 25 Google 20 Microsoft 15 Amazon 10
28.5K Views
Medium Frequency
~5 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen