6

Numbers With Same Consecutive Differences

Difficulty: HARD
Contributed By
Anish De
Avg. time to solve
50 min
Success Rate
50%

Problem Statement

You are given two integers ‘N’ and ‘K’. You need to return all non-negative integers of length N, such that the absolute difference between every two consecutive digits is ‘K’.

Note:
 Note that leading zeros of a number are not counted as digits. For example, 010, has two digits and not three.
For example :
Given N = 2, K = 4, so the list of numbers of length 2, having difference between digits 4 are : [15, 26, 37, 40, 48, 51, 59, 62, 73, 84, 95] 
Input Format:
The first line contains an integer ‘T’ which denotes the number of test cases or queries to be run. Then the test cases are as follows.

The first and the only line of each test case contains two space-separated integers, ‘N’ and ‘K’, denoting the length of integer and difference between consecutive digits.
Output Format:
For each test case, you need to print all the possible non-negative integers of length ‘N’, having a difference ‘K’ between two consecutive digits.

Print the possible numbers in sorted order.

Print the output of each test case in a separate line.
Note:
You don’t need to print anything; It has already been taken care of.
Constraints:
1 <= T <= 10
2 <= N <= 9
0 <= K <= 9

Time limit: 1 sec
Sample Input 1:
2
2 4
3 0
Sample Output 1:
15 26 37 40 48 51 59 62 73 84 95
111 222 333 444 555 666 777 888 999
Explanation For Sample Input 1:
In the first test case, 

N = 2, K = 4, so the list of numbers of length 2, having difference between digits 4 are : [15, 26, 37, 40, 48, 51, 59, 62, 73, 84, 95] 


In the second test case, 

N = 3, K = 0, so the list of numbers of length 3, having difference between digits 0 are : [111, 222, 333, 444, 555, 666, 777, 888, 999] 
Sample Input 2:
2
2 0
2 9
Sample Output 2:
11 22 33 44 55 66 77 88 99
90
Reset Code
Full screen
copy-code
Console