Super Palindromes - Problem

🎯 Super Palindromes Problem

A super-palindrome is a fascinating mathematical concept - it's a number that is both a palindrome and the square of another palindrome!

Given two positive integers left and right represented as strings, your task is to count how many super-palindromes exist in the inclusive range [left, right].

What makes a super-palindrome?

  • The number must be a palindrome (reads the same forwards and backwards)
  • It must be the perfect square of another palindrome

Example: The number 9 is a super-palindrome because:

  • 9 is a palindrome βœ…
  • 9 = 3Β² and 3 is also a palindrome βœ…

Your goal is to efficiently count all such numbers in the given range without checking every possible number!

Input & Output

example_1.py β€” Basic Range
$ Input: left = "1", right = "2"
β€Ί Output: 1
πŸ’‘ Note: Only 1 is a super-palindrome in range [1,2]. 1 is a palindrome and 1 = 1Β², where 1 is also a palindrome.
example_2.py β€” Larger Range
$ Input: left = "4", right = "1000"
β€Ί Output: 4
πŸ’‘ Note: Super-palindromes in [4,1000] are: 9 (3Β²), 121 (11Β²), 484 (22Β²), and 10201 would be next but exceeds 1000.
example_3.py β€” Edge Case
$ Input: left = "1", right = "1"
β€Ί Output: 1
πŸ’‘ Note: Range contains only 1, which is a super-palindrome (1 = 1Β² and both 1 and 1 are palindromes).

Visualization

Tap to expand
🌺 The Super-Palindrome Garden 🌺Palindromic Seeds131122Square!Super-Palindrome Flowers11Β² = 193Β² = 912111Β² = 12148422Β² = 484Garden Range [left, right]βœ“ Count flowers insideβœ— Ignore flowers outsideFinal Count: 4πŸ”‘ Key Insight: Generate palindromic roots, not all numbers!Instead of checking 10¹⁸ numbers, we check only √(right) β‰ˆ 10⁹ candidates
Understanding the Visualization
1
Plant Seeds
Generate palindromic 'seeds' (potential square roots) from 1 to √(right)
2
Square Formation
Arrange each seed in a perfect square pattern (calculate rootΒ²)
3
Check Bloom
See if the square arrangement also creates a palindromic pattern
4
Count Garden
Count how many beautiful super-palindromic flowers grew in our range
Key Takeaway
🎯 Key Insight: Super-palindromes are rare gems! Instead of searching through potentially 10¹⁸ numbers, we smartly generate only the palindromic 'seeds' (square roots) and test if their squares bloom into palindromic flowers. This reduces our search space dramatically from O(right) to O(√right)!

Time & Space Complexity

Time Complexity
⏱️
O(√(right) * L)

We generate palindromes up to √(right), and for each palindrome we perform string operations taking O(L) time where L is the number of digits

n
2n
βœ“ Linear Growth
Space Complexity
O(L)

Space for storing string representations of numbers, where L is the maximum number of digits

n
2n
βœ“ Linear Space

Constraints

  • 1 ≀ left ≀ right ≀ 1018
  • left and right are represented as strings
  • The range can be extremely large, making brute force impractical
  • Super-palindromes are relatively rare, especially for large numbers
Asked in
Google 15 Facebook 8 Microsoft 5
23.4K Views
Medium Frequency
~25 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