Given two integer **N** and **K**, the task is to print all positive numbers made up of **N **digits whose difference between the first and last digits equal to **K**.

**Examples:**

Input:N = 2, K = 0Output:11, 22, 33, 44, 55, 66, 77, 88, 99

Input:N = 2, K = 9Output:90

**Approach:** The idea is to generate all possible 1-digit numbers to **N-digit** numbers using recursion and check if the difference between the first and the last digit of that number is equal to **K** or not. Below are the steps:

- Generate all possible numbers with length 1.
- At each step, keep on adding digits to the number until the length of the number becomes
**N**. - When the length of the number becomes equal to
**N**, calculate the difference between the first and last digit of the number, and check if the difference is equal to**N**or not. If found to be true, print that number and proceed to generate the next number.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to store and check the` `// difference of digits` `void` `findNumbers(string st, vector<` `int` `>& result,` ` ` `int` `prev, ` `int` `n, ` `int` `K)` `{` ` ` `// Base Case` ` ` `if` `(st.length() == n) {` ` ` `result.push_back(stoi(st));` ` ` `return` `;` ` ` `}` ` ` `// Last digit of the number to` ` ` `// check the difference from the` ` ` `// first digit` ` ` `if` `(st.size() == n - 1) {` ` ` `// Condition to avoid` ` ` `// repeated values` ` ` `if` `(prev - K >= 0) {` ` ` `string pt = ` `""` `;` ` ` `// Update the string pt` ` ` `pt += prev - K + 48;` ` ` `// Recursive Call` ` ` `findNumbers(st + pt, result,` ` ` `prev - K, n, K);` ` ` `}` ` ` `if` `(K != 0 && prev + K < 10) {` ` ` `string pt = ` `""` `;` ` ` `// Update the string pt` ` ` `pt += prev + K + 48;` ` ` `// Recursive Call` ` ` `findNumbers(st + pt, result,` ` ` `prev + K, n, K);` ` ` `}` ` ` `}` ` ` `// Any number can come in between` ` ` `// first and last except the zero` ` ` `else` `{` ` ` `for` `(` `int` `j = 1; j <= 9; j++) {` ` ` `string pt = ` `""` `;` ` ` `pt += j + 48;` ` ` `// Recursive Call` ` ` `findNumbers(st + pt, result,` ` ` `prev, n, K);` ` ` `}` ` ` `}` `}` `// Function to place digit of the number` `vector<` `int` `> numDifference(` `int` `N, ` `int` `K)` `{` ` ` `vector<` `int` `> res;` ` ` `string st = ` `""` `;` ` ` `// When N is 1 and K > 0, then the` ` ` `// single number will be the first` ` ` `// and last digit it cannot have` ` ` `// difference greater than 0` ` ` `if` `(N == 1 && K == 0) {` ` ` `res.push_back(0);` ` ` `}` ` ` `else` `if` `(N == 1 && K > 0) {` ` ` `return` `res;` ` ` `}` ` ` `// This loop place the digit at the` ` ` `// starting` ` ` `for` `(` `int` `i = 1; i < 10; i++) {` ` ` `string temp = ` `""` `;` ` ` `temp += 48 + i;` ` ` `// Recursive Call` ` ` `findNumbers(st + temp, res, i, N, K);` ` ` `st = ` `""` `;` ` ` `}` ` ` `return` `res;` `}` `void` `numDifferenceUtil(` `int` `N, ` `int` `K)` `{` ` ` `// Vector to store results` ` ` `vector<` `int` `> res;` ` ` `// Generate all the resultant number` ` ` `res = numDifference(N, K);` ` ` `// Print the result` ` ` `for` `(` `int` `i = 0; i < res.size(); i++) {` ` ` `cout << res[i] << ` `" "` `;` ` ` `}` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `N = 2, K = 9;` ` ` `// Function Call` ` ` `numDifferenceUtil(N, K);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program for ` `# the above approach` `# Function to store and ` `# check the difference` `# of digits` `result ` `=` `[]` `def` `findNumbers(st, prev, ` ` ` `n, K): ` ` ` ` ` `global` `result` ` ` ` ` `# Base Case` ` ` `if` `(` `len` `(st) ` `=` `=` `n):` ` ` `result.append(` `int` `(st))` ` ` `return` ` ` `# Last digit of the number to` ` ` `# check the difference from the` ` ` `# first digit` ` ` `if` `(` `len` `(st) ` `=` `=` `n ` `-` `1` `):` ` ` ` ` `# Condition to avoid` ` ` `# repeated values` ` ` `if` `(prev ` `-` `K >` `=` `0` `):` ` ` `pt ` `=` `""` ` ` `# Update the string pt` ` ` `pt ` `+` `=` `prev ` `-` `K ` `+` `48` ` ` `# Recursive Call` ` ` `findNumbers(st ` `+` `pt, ` ` ` `prev ` `-` `K, ` ` ` `n, K)` ` ` `if` `(K !` `=` `0` `and` ` ` `prev ` `+` `K < ` `10` `):` ` ` `pt ` `=` `""` ` ` `# Update the string pt` ` ` `pt ` `+` `=` `prev ` `+` `K ` `+` `48` ` ` `# Recursive Call` ` ` `findNumbers(st ` `+` `pt, ` ` ` `prev ` `+` `K, ` ` ` `n, K)` ` ` `# Any number can come in between` ` ` `# first and last except the zero` ` ` `else` `:` ` ` `for` `j ` `in` `range` `(` `1` `, ` `10` `, ` `1` `):` ` ` `pt ` `=` `""` ` ` `pt ` `+` `=` `j ` `+` `48` ` ` `# Recursive Call` ` ` `findNumbers(st ` `+` `pt, ` ` ` `prev, n, K)` `# Function to place digit ` `# of the number` `def` `numDifference(N,K):` ` ` `global` `result` ` ` `st ` `=` `""` ` ` ` ` `# When N is 1 and K > 0, ` ` ` `# then the single number ` ` ` `# will be the first and ` ` ` `# last digit it cannot have` ` ` `# difference greater than 0` ` ` `if` `(N ` `=` `=` `1` `and` `K ` `=` `=` `0` `):` ` ` `result.append(` `0` `)` ` ` `elif` `(N ` `=` `=` `1` `and` `K > ` `0` `):` ` ` `return` `result` ` ` `# This loop place the ` ` ` `# digit at the starting` ` ` `for` `i ` `in` `range` `(` `1` `, ` `10` `, ` `1` `):` ` ` `temp ` `=` `""` ` ` `temp ` `+` `=` `str` `(` `48` `+` `i)` ` ` `# Recursive Call` ` ` `findNumbers(st ` `+` `temp, ` ` ` `i, N, K)` ` ` `st ` `=` `""` ` ` `return` `result` `def` `numDifferenceUtil(N, K):` ` ` ` ` `# Vector to store results` ` ` `res ` `=` `[]` ` ` `# Generate all the` ` ` `# resultant number` ` ` `res ` `=` `numDifference(N, K)` ` ` `# Print the result` ` ` `for` `i ` `in` `range` `(` `1` `, ` `len` `(res)):` ` ` `print` `(res[i] ` `+` `40` `, ` ` ` `end ` `=` `" "` `)` ` ` `break` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` ` ` `N ` `=` `2` ` ` `K ` `=` `9` ` ` ` ` `# Function Call` ` ` `numDifferenceUtil(N, K)` ` ` `# This code is contributed by bgangwar59` |

*chevron_right*

*filter_none*

**Output:**

90

**Time Complexity:** O(2^{N}) **Auxiliary Space:** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count numbers in a range with digit sum divisible by K having first and last digit different
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Count of Numbers in Range where first digit is equal to last digit of the number
- Count of N-digit numbers having digit XOR as single digit
- Last digit of a number raised to last digit of N factorial
- Generate all N digit numbers having absolute difference as K between adjacent digits
- Count non-palindromic array elements having same first and last digit
- Find the remainder when First digit of a number is divided by its Last digit
- Count of pairs (A, B) in range 1 to N such that last digit of A is equal to the first digit of B
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Absolute difference between the first X and last X Digits of N
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count all numbers up to N having M as the last digit
- Find a number K having sum of numbers obtained by repeated removal of last digit of K is N
- Count numbers with same first and last digits
- Count n digit numbers not having a particular digit
- Count numbers formed by given two digit with sum having given digits
- Count of N digit Numbers having no pair of equal consecutive Digits
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.