Find the Count of Good Integers - 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 by k

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: Any integer must not have leading zeros, neither before nor after rearrangement. For example, 1010 cannot be rearranged to form 101.

Input & Output

Example 1 — Basic Case
$ Input: n = 3, k = 5
Output: 27
💡 Note: For 3-digit numbers, we need palindromes like ABA where A∈{1-9}, B∈{0-9}. Count arrangements that form palindromes divisible by 5.
Example 2 — Single Digit
$ Input: n = 1, k = 4
Output: 2
💡 Note: Single digits divisible by 4: only 4 and 8. So count is 2.
Example 3 — Even Length
$ Input: n = 2, k = 6
Output: 4
💡 Note: 2-digit palindromes divisible by 6: must be divisible by both 2 and 3. Examples: 66 (rearrangement of 66).

Constraints

  • 1 ≤ n ≤ 15
  • 1 ≤ k ≤ 109

Visualization

Tap to expand
Find Count of Good Integers: n=3, k=5Inputn = 3 digitsk = 5 divisibleGood IntegerExample: 150Can form: 105k-Palindromic105 is palindrome105 ÷ 5 = 21 ✓CountAll validintegersPattern: ABA where A∈{1,2,...,9}, B∈{0,1,...,9}A choices: 9B choices: 10Div by 5: filterPalindromes ending in 0 or 5 are divisible by 5Result: Count all valid digit combinations
Understanding the Visualization
1
Input
n=3 digits, k=5 (divisibility requirement)
2
Process
Find digit arrangements that form palindromes divisible by 5
3
Output
Count of good integers
Key Takeaway
🎯 Key Insight: Generate palindrome digit patterns and use combinatorics to count valid arrangements without explicit enumeration
Asked in
Google 15 Microsoft 12
12.5K Views
Medium Frequency
~45 min Avg. Time
280 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