Find the Count of Good Integers - Problem
The Challenge: You need to find how many good integers exist with exactly
An integer is k-palindromic if:
• It reads the same forwards and backwards (palindrome)
• It's divisible by
An integer is good if you can rearrange its digits to form a k-palindromic number.
Examples:
• For k=2:
• For k=2:
Important: No leading zeros allowed, before or after rearrangement!
n digits.An integer is k-palindromic if:
• It reads the same forwards and backwards (palindrome)
• It's divisible by
kAn integer is good if you can rearrange its digits to form a k-palindromic number.
Examples:
• For k=2:
2020 → 2002 (palindrome ✓, divisible by 2 ✓)• For k=2:
1010 → No valid arrangement (can't form palindrome divisible by 2)Important: No leading zeros allowed, before or after rearrangement!
Input & Output
example_1.py — Basic Case
$
Input:
n = 3, k = 5
›
Output:
1
💡 Note:
For n=3 digits and k=5: Valid palindromes divisible by 5 must end in 0 or 5. The palindrome "505" works, giving us digit pattern {0:1, 5:2}. This represents 1 good integer pattern.
example_2.py — Even Length
$
Input:
n = 4, k = 2
›
Output:
4
💡 Note:
For n=4 digits and k=2: Even-digit palindromes divisible by 2 must be even. Examples include "1001", "1111", "1221", "1331", etc. After counting unique digit patterns, we get 4 different patterns.
example_3.py — Edge Case
$
Input:
n = 1, k = 1
›
Output:
9
💡 Note:
For n=1 digit and k=1: All single digits 1-9 are palindromes divisible by 1 (no leading zeros allowed). Each represents a unique pattern, so answer is 9.
Visualization
Tap to expand
Understanding the Visualization
1
Half Production
Generate all possible first halves (much fewer than full numbers)
2
Mirror Assembly
Complete each palindrome by mirroring the first half
3
Quality Control
Test if the palindrome is divisible by k
4
Pattern Catalog
Count unique digit frequency patterns
Key Takeaway
🎯 Key Insight: Generate palindromes systematically from their first half instead of checking all possible numbers. This reduces the search space exponentially while guaranteeing we find all valid patterns.
Time & Space Complexity
Time Complexity
O(10^(n/2))
Generate O(10^(n/2)) palindromes, constant time combinatorial calculation
✓ Linear Growth
Space Complexity
O(1)
Only store current palindrome and digit counts
✓ Linear Space
Constraints
- 1 ≤ n ≤ 10
- 1 ≤ k ≤ 9
- All palindromes must have exactly n digits
- No leading zeros allowed before or after rearrangement
- Count unique digit patterns, not individual numbers
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code