Given two integers n and k, return an array of all the integers of length n
where the difference between every two consecutive digits is k
. You may return the answer in any order.
Note that the integers should not have leading zeros. Integers as 02
and 043
are not allowed.
Example 1:
Input: n = 3, k = 7 Output: [181,292,707,818,929] Explanation: Note that 070 is not a valid number, because it has leading zeroes.
Example 2:
Input: n = 2, k = 1 Output: [10,12,21,23,32,34,43,45,54,56,65,67,76,78,87,89,98]
Constraints:
2 <= n <= 9
0 <= k <= 9
When 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 method creates all possible numbers that meet our requirements. We'll start by building numbers digit by digit, checking if the difference between adjacent digits matches what we want. We'll keep only the numbers that fit the requested length and meet the difference criteria.
Here's how the algorithm would work step-by-step:
def numbers_with_same_consecutive_differences(number_length, difference):
results = []
# Iterate through single digit numbers as starting points
for starting_digit in range(1, 10):
def generate_numbers(current_number):
if len(str(current_number)) == number_length:
results.append(current_number)
return
last_digit = int(str(current_number)[-1])
# Check possible next digits based on the difference
possible_next_digits = [last_digit + difference, last_digit - difference]
for next_digit in possible_next_digits:
# Validate next digit to be from 0 to 9
if 0 <= next_digit <= 9:
generate_numbers(current_number * 10 + next_digit)
generate_numbers(starting_digit)
# Remove duplicate numbers
return list(set(results))
The best way to solve this is to build the numbers one digit at a time, exploring all valid options at each step. This allows us to avoid generating numbers that don't meet the criteria early on, making the whole process much faster.
Here's how the algorithm would work step-by-step:
def numbers_with_same_consecutive_differences(number_length, difference):
results = set()
def generate_numbers(current_number, remaining_length):
if remaining_length == 0:
results.add(current_number)
return
last_digit = current_number % 10
possible_next_digits = []
if last_digit + difference <= 9:
possible_next_digits.append(last_digit + difference)
if last_digit - difference >= 0:
possible_next_digits.append(last_digit - difference)
for next_digit in possible_next_digits:
generate_numbers(current_number * 10 + next_digit, remaining_length - 1)
# Iterate through single digit numbers to start the sequence.
for starting_digit in range(1, 10):
generate_numbers(starting_digit, number_length - 1)
# Remove leading zeros while maintaining uniqueness.
final_results = sorted(list(results))
return final_results
Case | How to Handle |
---|---|
N is 0 | Return an empty list as no digits can be formed. |
K is larger than 9 or less than -9 | If K is outside the valid digit difference range, the generator will produce an empty list or only single digit numbers if N is 1; otherwise it will produce no results. |
N is a very large number (e.g., 10 or greater) | The recursive approach needs optimization to avoid excessive call stack depth which will lead to stack overflow errors; memoization might be an option. |
K is 0 | The solution should generate numbers with all identical digits. |
N is 1 | Return a list of digits from 0 to 9. |
Leading zeros for N > 1 (e.g., '01', '02' are not valid) | Ensure that the first digit of the generated number is not zero unless N=1. |
Integer overflow when constructing the number | Use a data type capable of holding larger numbers, such as `long` or perform checks to prevent overflow before appending digits; consider using string representation to handle numbers of large magnitude instead of int. |
All numbers from 1 to 9, with all the same difference | The algorithm generates all possibilities based on the given K, so it will correctly handle this case. |