Skip to main content
Back to problems
Leetcode
Medium
Combinatorics
Backtracking
Math
Hash Maps
Find The Count Of Good Integers

Count how many length-nn integers can be rearranged into a palindrome that is divisible by kk.

Acceptance 0%
Problem Statement

Problem

You are given two integers nn and kk.

Count how many positive integers with exactly nn digits are good.

An nn-digit integer is called good if its digits can be rearranged to form a palindrome that is divisible by kk.

Two integers are considered different if their decimal representations are different, even if they contain the same digits in a different order.

Notes

  • The integer must have exactly nn digits, so it cannot start with 0.
  • A palindrome reads the same forward and backward.
  • You only need to count integers that can be rearranged into at least one valid palindrome divisible by kk.

Input Format

  • Two integers n and k.

Output Format

  • Return the number of good integers with exactly n digits.

Constraints

  • 1n101 \le n \le 10
  • 1k91 \le k \le 9
  • Digits are decimal (0 to 9).

Hints

  • For a palindrome, the multiset of digits is highly constrained: at most one digit may have an odd frequency.
  • Instead of checking every integer directly, think in terms of digit counts and generate only palindromic candidates.
  • Once a valid multiset of digits is found, count how many nn-digit numbers can be formed from it without leading zeros.

Input Format

Two integers n and k.

Output Format

The count of good integers with exactly n digits.

Constraints

  • 1n101 \le n \le 10
  • 1k91 \le k \le 9
  • The integer must have exactly nn digits.
  • Leading zeros are not allowed in the integer representation.
Examples
Sample cases returned by the problem API.

Example 1

Input

n = 3, k = 2

Output

9

Explanation

The good 3-digit integers are exactly those whose digits can be rearranged into an even palindrome divisible by 2, such as 202, 212, 222, 232, 242, 252, 262, 272, and 282. All of them have digit multiset {2, x, 2} with a valid palindrome arrangement divisible by 2, giving a total of 9.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.