Problem
You are given two positive integers n
and k
.
An integer x
is called k-palindromic if:
x
is a palindrome.x
is divisible byk
.
An integer is called good if its digits can be rearranged to form a k-palindromic integer. For example, for k = 2
, 2020 can be rearranged to form the k-palindromic integer 2002, whereas 1010 cannot be rearranged to form a k-palindromic integer.
Return the count of good integers containing n
digits.
Note that any integer must not have leading zeros, neither before nor after rearrangement. For example, 1010 cannot be rearranged to form 101.
Examples
Example 1:
|
|
Example 2:
|
|
Example 3:
|
|
Constraints:
1 <= n <= 10
1 <= k <= 9
Solution
Method 1 - Enumeration + Permutation and Combination
The problem revolves around counting good integers composed of n
digits, where a good integer is defined as:
- It can be rearranged to form a k-palindromic integer.
- A k-palindromic integer is a number that:
- Is a palindrome (i.e., reads the same forwards and backwards).
- Is divisible by
k
.
- It must have no leading zeros after rearrangement.
The key insight is to break the problem into manageable sections by leveraging palindromic symmetry and efficient combinatorial calculations.
Approach
Our approach consists of the following key steps:
Step 1: Generate all valid n-digit Palindromes
- Palindromes are symmetrical numbers. Construct them using:
- Base digits: Starting from
10^(ceil(n/2) - 1)
to10^(ceil(n/2))
, where the first half of the palindrome is iterated (e.g.,abc
orab
). - Reflection with adjustments:
- If
n
is odd, exclude the middle digit in reflection. - For example:
- Odd
n=5
:abc|cba
. - Even
n=4
:ab|ba
.
- Odd
- If
- Base digits: Starting from
Step 2: Filter k-palindromic numbers
- Check each generated palindrome to see if it is divisible by
k
. - If it is divisible, sort its digits and store the sorted form in a
Set
to eliminate duplicates caused by rearrangements:- For example:
12021
and21012
will both sort to"01122"
.
- For example:
Step 3: Precompute factorials
- Precompute factorials up to
n
to optimise calculations for permutations and combinations. - Factorials help compute the number of unique ways an integer can be rearranged using its frequency counts.
Step 4: Count valid good integers
- For each unique digit combination in the
Set
, compute:- Total permutations of the digits with:
- Leading zeros removed.
- Factorial division for repetitive digits (e.g., for
112
, divide by2!
for the two 1s).
- Final count:
- Calculate valid permutations for each digit group and sum them to find the total number of good integers.
- Total permutations of the digits with:
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(10^(n/2) * n)
- Palindrome generation:
O(10^(n/2))
, as only half the digit space is iterated. - Sorting/digit manipulation:
O(n * log(n))
for each valid palindrome. - Permutation calculation:
O(n)
for each valid palindrome using precomputed factorials. - Overall:
O(10^(n/2) * n)
.
- Palindrome generation:
- 🧺 Space complexity:
O(10^(n/2))
- Factorial computation:
O(n)
. - Palindrome storage: proportional to
10^(n/2)
for valid palindromes stored.
- Factorial computation: