Generate all N digit numbers having absolute difference as K between adjacent digits
Last Updated :
29 May, 2021
Given two integers N and K, the task is to generate all positive integers with length N having an absolute difference of adjacent digits equal to K.
Examples:
Input: N = 4, K = 8
Output: 1919, 8080, 9191
Explanation:
The absolute difference between every consecutive digit of each number is 8.
For Example: 8080 (abs(8-0) = 8, abs(0-8) = 8)
Input: N = 2, K = 2
Output: 13, 24, 20, 35, 31, 46, 42, 57, 53, 68, 64, 79, 75, 86, 97
Explanation:
The absolute difference between every consecutive digit of each number is 1.
Approach: The idea is to use Backtracking. Iterate over digits [1, 9] and for each digit from the N-digit number having a difference of absolute digit as K using recursion. Below are the steps:
1. Create a vector ans[] to store all the resulting numbers and recursively iterate from 1 to 9 to generate numbers starting from each digit. Below are the cases:
- Base Case: For all integers of a single length, that is, N = 1, add them to ans[].
- Recursive Call: If adding digit K to the numbers to one’s digit doesn’t exceed 9, then recursively call by decreasing the N and update num to (10*num + num%10 + K) as shown below:
if(num % 10 + K ? 9) {
recursive_function(10 * num + (num % 10 + K), K, N – 1, and);
}
- If the value of K is non-zero after all the recursive call and if num % 10 >= K, then again recursively call by decreasing the N and update num to (10*num + num%10 – K) as:
recursive_function(10 * num + num % 10 – K, K, N – 1, ans);
2. After all the above steps print the numbers stored in ans[].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void checkUntil( int num, int K,
int N, vector< int >& ans)
{
if (N == 1)
{
ans.push_back(num);
return ;
}
if ((num % 10 + K) <= 9)
checkUntil(10 * num
+ (num % 10 + K),
K, N - 1, ans);
if (K) {
if ((num % 10 - K) >= 0)
checkUntil(10 * num
+ num % 10 - K,
K, N - 1,
ans);
}
}
void check( int K, int N, vector< int >& ans)
{
for ( int i = 1; i <= 9; i++) {
checkUntil(i, K, N, ans);
}
}
void print(vector< int >& ans)
{
for ( int i = 0; i < ans.size(); i++) {
cout << ans[i] << ", " ;
}
}
int main()
{
int N = 4, K = 8;
vector< int > ans;
check(K, N, ans);
print(ans);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void checkUntil( int num, int K, int N,
Vector<Integer> ans)
{
if (N == 1 )
{
ans.add(num);
return ;
}
if ((num % 10 + K) <= 9 )
checkUntil( 10 * num + (num % 10 + K),
K, N - 1 , ans);
if (K > 0 )
{
if ((num % 10 - K) >= 0 )
checkUntil( 10 * num + num % 10 - K,
K, N - 1 , ans);
}
}
static void check( int K, int N, Vector<Integer> ans)
{
for ( int i = 1 ; i <= 9 ; i++)
{
checkUntil(i, K, N, ans);
}
}
static void print(Vector<Integer> ans)
{
for ( int i = 0 ; i < ans.size(); i++)
{
System.out.print(ans.get(i) + ", " );
}
}
public static void main(String[] args)
{
int N = 4 , K = 8 ;
Vector<Integer> ans = new Vector<Integer>();;
check(K, N, ans);
print(ans);
}
}
|
Python3
def checkUntil(num, K, N, ans):
if (N = = 1 ):
ans.append(num)
return
if ((num % 10 + K) < = 9 ):
checkUntil( 10 * num +
(num % 10 + K),
K, N - 1 , ans)
if (K):
if ((num % 10 - K) > = 0 ):
checkUntil( 10 * num +
num % 10 - K,
K, N - 1 , ans)
def check(K, N, ans):
for i in range ( 1 , 10 ):
checkUntil(i, K, N, ans)
def print_list(ans):
for i in range ( len (ans)):
print (ans[i], end = ", " )
if __name__ = = "__main__" :
N = 4
K = 8 ;
ans = []
check(K, N, ans)
print_list(ans)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void checkUntil( int num, int K, int N,
List< int > ans)
{
if (N == 1)
{
ans.Add(num);
return ;
}
if ((num % 10 + K) <= 9)
checkUntil(10 * num + (num % 10 + K),
K, N - 1, ans);
if (K > 0)
{
if ((num % 10 - K) >= 0)
checkUntil(10 * num + num % 10 - K,
K, N - 1, ans);
}
}
static void check( int K, int N, List< int > ans)
{
for ( int i = 1; i <= 9; i++)
{
checkUntil(i, K, N, ans);
}
}
static void print(List< int > ans)
{
for ( int i = 0; i < ans.Count; i++)
{
Console.Write(ans[i] + ", " );
}
}
public static void Main(String[] args)
{
int N = 4, K = 8;
List< int > ans = new List< int >();;
check(K, N, ans);
print(ans);
}
}
|
Javascript
<script>
function checkUntil(num, K, N,
ans)
{
if (N == 1)
{
ans.push(num);
return ;
}
if ((num % 10 + K) <= 9)
checkUntil(10 * num + (num % 10 + K),
K, N - 1, ans);
if (K > 0)
{
if ((num % 10 - K) >= 0)
checkUntil(10 * num + num % 10 - K,
K, N - 1, ans);
}
}
function check(K, N, ans)
{
for (let i = 1; i <= 9; i++)
{
checkUntil(i, K, N, ans);
}
}
function print( ans)
{
for (let i = 0; i < ans.length; i++)
{
document.write(ans[i] + ", " );
}
}
let N = 4, K = 8;
let ans = []
check(K, N, ans);
print(ans);
</script>
|
Output:
1919, 8080, 9191,
Time Complexity: O(2N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...