0

Numbers With Same Consecutive Differences

Difficulty: EASY
Avg. time to solve
20 min
Success Rate
80%

Problem Statement
Suggest Edit

You are given two integers, ‘n’ and ‘k’. The task is to return all non-negative integers having ‘n’ digits such that the absolute difference between each consecutive digit is ‘k’.

Example:
Input: n = 2, k = 6
We need to return all two-digit numbers where the absolute difference between each consecutive digit is ‘6’. Note that ‘06’ is not a valid two-digit number. So, the answer is:
Output: [17, 28, 39, 60, 71, 82, 93]
Note:
1. Every number in the answer should not contain leading zeroes. E.g., the number ‘09’ is invalid.
2. You can return the answer in any order.
3. You do not need to print anything; it has already been taken care of. Just implement the function.
Input format:
The first line of input contains an integer ‘T’ which denotes the number of test cases. Then, the ‘T’ test cases follow.

The first and only line of each test case contains two space-separated integers, ‘n’ and ‘k’, denoting the number of digits and the absolute difference between each consecutive digit.
Output format:
For every test case, return an array of integers where each integer has ‘n’ digits, and the absolute difference between each digit is ‘k’. You may return output in any order, but the printed output will be in sorted order.
Constraints:
1 <= T <= 100
2 <= n <= 9
0 <= k <= 9

Where ‘T’ is the number of test cases, ‘n’ is the number of digits, and ‘k’ is the absolute difference between each consecutive digit.

Time limit: 1 second

Sample input 1:

2
2 0
3 6

Sample output 1:

11 22 33 44 55 66 77 88 99
171 282 393 606 717 828 939

Explanation of sample input 1:

Test Case 1:
We need to return all two-digit numbers where the absolute difference between each consecutive digit is ‘0’. So, the answer is:
Output: [11, 22, 33, 44, 55, 66, 77, 88, 99]

Test Case 2:
We need to return all three-digit numbers where the absolute difference between each consecutive digit is ‘6’. So, the answer is:
Output: [171, 282, 393, 606, 717, 828, 939]

Sample input 2:

2
4 9
2 3

Sample output 2:

9090
14 25 30 36 41 47 52 58 63 69 74 85 96
Reset Code
Full screen
copy-code
Console