2396. Strictly Palindromic Number

Problem Description

An integer is classified as strictly palindromic if it adheres to a special rule regarding its representation in different number bases. Specifically, a number n is strictly palindromic if, for every base b starting from 2 up to n - 2, its representation in that base is a palindrome. It's important to note that a palindrome reads the same forward and backward. For instance, the string "121" is a palindrome because it remains "121" even when reversed.

The task is to create a function that takes an integer n and returns true if n is strictly palindromic or false otherwise. This means the function needs to verify if n is a palindrome in all the bases from 2 to n - 2. The challenge is to figure out a way to evaluate this condition effectively or to determine if such a number can exist.


Upon initial consideration, you might think that to determine if a number is strictly palindromic, you'd need to convert the number into all the base representations from 2 to n-2 and check if it's a palindrome in each. This would indeed be the brute-force approach.

However, a closer examination of the problem can lead to the conclusion that such a number cannot exist. Intuitively, the notion of a strictly palindromic number is flawed because when a number n is represented in base n-1, the result would always be 11 (since n base n-1 is the same as saying n-1 times 1 with a remainder of 1, which is how base conversion works). This representation of the number n as 11 in base n-1 is palindromic, but that is just for this single base.

When you get to base n-2, the hypothesis falls apart. Regardless of what the number n is, it cannot continue to be palindromic in each and every base leading down to 2 from n-2. This is due to the way numbers scale in different bases, which inevitably leads to different least significant digits as you go down in base, disrupting the palindromic nature.

Given these considerations, it turns out that the solution to the problem is quite simple: no number greater than 3 can satisfy the condition of being strictly palindromic because it will fail the test when n is expressed in base n-2. The function, therefore, correctly always returns False, simplifying the solution significantly.

Learn more about Math and Two Pointers patterns.

Not Sure What to Study? Take the 2-min Quiz to Find Your Missing Piece:

Which of the following is the prefix sum of array [1, 2, 3, 4, 5]?

Solution Approach

The implementation of the solution does not require complex algorithms, data structures, or patterns. It is based on the mathematical proof that there are no strictly palindromic numbers beyond 3 due to the nature of base conversion, particularly when n is represented in base n - 2.

Exploring different bases:

  • When converting a number to a base that is one less than the number itself (base n - 1), the number is always represented as 11. This is palindromic, but it only works for that specific base.
  • Reducing the base further to n - 2, the representation changes and cannot maintain the palindromic property across all lower bases down to 2.

Given this understanding, the reference solution's approach is straightforward:

The isStrictlyPalindromic function in Python is defined with a single line of code:

1class Solution:
2    def isStrictlyPalindromic(self, n: int) -> bool:
3        return False

This function does not require any loops, checks, or base conversions because it leverages the knowledge that no number n > 3 can ever be strictly palindromic. Thus, the solution is exceptionally efficient with a constant time complexity of O(1) - it will always return False immediately, regardless of the value of n.

Discover Your Strengths and Weaknesses: Take Our 2-Minute Quiz to Tailor Your Study Plan:

Consider the classic dynamic programming of longest increasing subsequence:

Find the length of the longest subsequence of a given sequence such that all elements of the subsequence are sorted in increasing order.

For example, the length of LIS for [50, 3, 10, 7, 40, 80] is 4 and LIS is [3, 7, 40, 80].

What is the recurrence relation?

Example Walkthrough

Let's pick an integer n and walk through the solution approach using this example. Suppose we choose n = 10. We need to determine if 10 is strictly palindromic, which means it must be a palindrome from base 2 up to base n - 2, which would be base 8 in this case.

  • For base 2, the representation of 10 is 1010, which is not palindromic.
  • For base 3 to base 7, the representation will change for each base, and while we might find it palindromic in one or a few bases, that is irrelevant because we need it to be palindromic in all bases from 2 to n - 2.
  • For base n - 1, which is base 9 for our example, the representation of 10 would be 11, as 10 in base 9 equals 9 + 1. This is indeed palindromic but again does not fulfill the strict condition required.
  • Finally, for base n - 2, which is base 8 in our case, the representation of 10 would no longer be 11. In base 8, 10 is equal to 12, because 10 in base 8 equals 8 + 2. This violates the condition to be strictly palindromic as it is not the same forward and backward.

From this example, we can observe that as we change the base, the representation of the number changes and does not remain palindromic in all bases leading up to n - 2. Hence, by similar logic, we can generalize that for any number n > 3, it will not remain strictly palindromic across all the bases from 2 to n - 2. The solution below reflects this understanding:

1class Solution:
2    def isStrictlyPalindromic(self, n: int) -> bool:
3        return False

In summary, our example demonstrates that regardless of the number n we choose (provided n > 3), it will not be possible for it to be strictly palindromic. Hence, the function always returns False, as established by the solution approach detailed in the content provided.

Solution Implementation

1class Solution:
2    def isStrictlyPalindromic(self, n: int) -> bool:
3        # Explanation:
4        # The following method checks if the given number 'n' is strictly palindromic.
5        # However, upon review, it is found that this method simply returns False for any input.
6        # The term "strictly palindromic" is not well-defined in this context,
7        # because it does not check the number 'n' in any base to determine whether it is a palindrome.
8        # It is worth noting that by the traditional definition, no number can be strictly palindromic,
9        # as it would have to be a palindrome in every base, which is not possible for integers greater than 0.
10        # Thus, this function always returns False, suggesting that no number can be 'strictly' palindromic according to the unwritten rule.
12        return False
1class Solution {
3    // This method checks if an integer n is "strictly palindromic"
4    // According to the current definition provided, it returns false for all inputs
5    public boolean isStrictlyPalindromic(int n) {
6        // Always returns false as no integer can be strictly palindromic by the problem's original statement
7        return false;
8    }
1class Solution {
3    // Function to determine if a number is strictly palindromic
4    // Since the problem states to convert the number into all bases
5    // between 2 and n - 2 and check if they are palindromes, and no number
6    // can satisfy this condition, the function always returns false.
7    bool isStrictlyPalindromic(int n) {
8        // The implementation of the logic that eventually leads to the conclusion that
9        // no number can satisfy the strictly palindromic condition is omitted.
10        // Therefore, the function simply returns false.
11        return false;
12    }
2 * Checks if a number is strictly palindromic.
3 * A number is strictly palindromic if it forms a palindrome in every base from 2 up to n - 1
4 *
5 * @param {number} n - The number to check.
6 * @returns {boolean} - A boolean indicating if the number is strictly palindromic or not.
7 */
8function isStrictlyPalindromic(n: number): boolean {
9    // Iterate through all the bases from 2 to n - 1
10    for (let base = 2; base < n; base++) {
11        // Convert the number to the current base
12        const numberInBase = n.toString(base);
14        // Check if the converted number is a palindrome
15        if (!isPalindrome(numberInBase)) {
16            return false;
17        }
18    }
20    // If number n is palindromic in all bases from 2 to n - 1, return true
21    return true;
25 * Checks if a string is a palindrome.
26 *
27 * @param {string} str - The string to check.
28 * @returns {boolean} - A boolean indicating whether the string is a palindrome or not.
29 */
30function isPalindrome(str: string): boolean {
31    // Define pointers for the start and end of the string
32    let start = 0;
33    let end = str.length - 1;
35    // Check if the string is a palindrome by comparing characters from both ends
36    while (start < end) {
37        if (str[start] !== str[end]) {
38            return false;
39        }
40        start++;
41        end--;
42    }
44    // If all characters match, the string is a palindrome
45    return true;
Not Sure What to Study? Take the 2-min Quiz:

Breadth first search can be used to find the shortest path between two nodes in a directed graph.

Time and Space Complexity

The given code is a Python method within a class Solution that always returns False regardless of the input n.

Time Complexity: O(1)

The time complexity of the method isStrictlyPalindromic is constant time O(1), because the function performs a single operation (returning False) regardless of the size or value of the input parameter n.

Space Complexity: O(1)

The space complexity of the method is also constant O(1). No additional space is used that grows with the input size; the only space used is for the input itself and the return value, which do not depend on the value or size of n.

Learn more about how to find time and space complexity quickly using problem constraints.

Fast Track Your Learning with Our Quick Skills Quiz:

Problem: Given a list of tasks and a list of requirements, compute a sequence of tasks that can be performed, such that we complete every task once while satisfying all the requirements.

Which of the following method should we use to solve this problem?

Recommended Readings

Got a question? Ask the Teaching Assistant anything you don't understand.

Still not clear? Ask in the Forum,  Discord or Submit the part you don't understand to our editors.

TA 👨‍🏫