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 that any integer must not have leading zeros, neither before nor after rearrangement. For example, 1010 cannot be rearranged to form 101.
Example 1:
Input: n = 3, k = 5
Output: 27
Explanation:
Some of the good integers are:
Example 2:
Input: n = 1, k = 4
Output: 2
Explanation:
The two good integers are 4 and 8.
Example 3:
Input: n = 5, k = 6
Output: 2468
Constraints:
1 <= n <= 101 <= k <= 9When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to counting good integers is like checking every single number within the given range. We look at each number individually and determine if it fits our definition of a 'good' integer.
Here's how the algorithm would work step-by-step:
def count_good_integers_brute_force(start_number, end_number): good_integers_count = 0
# Iterate through each number in the specified range.
for current_number in range(start_number, end_number + 1):
# Check if the current number is a 'good' integer.
if is_good_integer(current_number):
good_integers_count += 1
return good_integers_count
def is_good_integer(number):
number_string = str(number)
# Good integers consist only of digits 1, 2, and 3.
for digit_character in number_string:
digit = int(digit_character)
if digit < 1 or digit > 3:
return False
return TrueThe goal is to count numbers within a given range that only use certain digits. Instead of checking every single number in the range, we can be smarter by constructing 'good' numbers digit by digit, making sure we only use allowed digits.
Here's how the algorithm would work step-by-step:
def find_the_count_of_good_integers(
lower_bound, upper_bound, allowed_digits):
allowed_digits = sorted([int(digit) for digit in allowed_digits])
def count_good_integers_less_than_or_equal_to(
number, allowed_digits):
number_as_string = str(number)
number_length = len(number_as_string)
count = 0
# Count 'good' numbers with fewer digits than the given number.
for length in range(1, number_length):
count += len(allowed_digits) ** length
# Build numbers with the same number of digits as the given number.
for index, digit in enumerate(number_as_string):
digit = int(digit)
valid_digits = [d for d in allowed_digits if d < digit]
count += len(valid_digits) * (
len(allowed_digits) ** (number_length - index - 1))
# If current digit not in allowed, stop.
if digit not in allowed_digits:
return count
# If we reach the last digit, the number itself is valid.
if index == number_length - 1:
count += 1
return count
# Handle cases where bounds are equal to zero
lower_bound_count = (
count_good_integers_less_than_or_equal_to(
lower_bound - 1, allowed_digits)
if lower_bound > 1 else 0
)
# Subtract the count of good integers below lower bound to be inclusive.
upper_bound_count = (
count_good_integers_less_than_or_equal_to(
upper_bound, allowed_digits))
# Final calculation. We subtract to find count within the range.
return upper_bound_count - lower_bound_count| Case | How to Handle |
|---|---|
| Input 'n' is zero or negative. | Return 0 since a non-positive n indicates no valid integers can be formed. |
| Input 'n' is a single digit (0-9). | Return n+1, as all digits from 0 to n are considered 'good'. |
| Input 'n' has repeating digits, such as 33 or 777. | The algorithm must correctly handle these cases without double-counting. |
| Input 'n' contains digits not in the set {0, 1, 2, 5, 6, 8, 9}. | The recursive/iterative function must skip over or adjust the bound properly when checking a disallowed digit. |
| Input 'n' is a large number that approaches integer limits. | Ensure no integer overflow occurs during calculations or comparisons, potentially using long data types if needed. |
| n starts with an invalid digit (3,4,7). | The starting digit must be converted to the next valid digit to continue processing (e.g., 3->5). |
| n consists entirely of 'good' digits (e.g., 111, 222, 555). | The code counts these cases accurately as they are valid according to the definition. |
| All digits of 'n' are greater than 2, such as '9865'. | The algorithm must iterate and count through all combinations of good numbers less than each digit. |