Find the Count of Good Integers - Problem
The Challenge: You need to find how many good integers exist with exactly n digits.

An integer is k-palindromic if:
• It reads the same forwards and backwards (palindrome)
• It's divisible by k

An integer is good if you can rearrange its digits to form a k-palindromic number.

Examples:
• For k=2: 20202002 (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
Half Generator10^(n/2) halvese.g., 10, 11, 12...Mirror Station12 → 12112 → 1221Quality Check1221 % k == 0?✓ or ✗Pattern Counter{1:2, 2:2}Unique patternsFAST!Factory Efficiency: O(10^(n/2)) vs O(10^n)For n=6: 1,000 operations vs 1,000,000 operations!
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

n
2n
Linear Growth
Space Complexity
O(1)

Only store current palindrome and digit counts

n
2n
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
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28
52.3K Views
High Frequency
~35 min Avg. Time
1.8K 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