Count of N digit numbers having absolute difference between adjacent digits as K
Last Updated :
16 Dec, 2021
Given two integers N and K. The task is to count all positive integers with length N having an absolute difference between adjacent digits equal to K.
Examples:
Input: N = 4, K = 8
Output: 3
Explanation: The absolute difference between every consecutive digit of each number is 8. Three possible numbers are 8080, 1919 and 9191.
Input: N = 2, K = 0
Output: 9
Explanation: 11, 22, 33, 44, 55, 66, 77, 88, 99. The absolute difference between every consecutive digit of each number is 0.
Approach: The approach is based on recursion. Iterate over digits [1, 9], and for each digit, count the N-digit number having a difference of absolute digit as K using recursion. Following cases arrive in the recursive function call.
- Base Case: For all single-digit integers i.e. N = 1, increment answer count.
- Recursive Call: If adding digit K to the one’s digit of the number formed till now (num) does not exceed 9, then recursively call by decreasing N and making num = (num*10 + num%10 + K).
if(num % 10 + K ≤ 9)
recursive_function(10 * num + (num % 10 + K), N – 1);
- If the value of K is non-zero after all the recursive calls and if num % 10 ≥ K, then again recursively call by decreasing the N and update num to (10*num + num%10 – K).
if(num % 10 ≥ K)
recursive_function(10 * num + num % 10 – K, N – 1)
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int countNums = 0;
void checkUtil( int num, int K, int N)
{
if (N == 1) {
countNums++;
return ;
}
if ((num % 10 + K) <= 9)
checkUtil(10 * num +
(num % 10 + K), K, N - 1);
if (K) {
if ((num % 10 - K) >= 0)
checkUtil(10 * num +
num % 10 - K, K, N - 1);
}
}
void check( int K, int N)
{
for ( int i = 1; i <= 9; i++) {
checkUtil(i, K, N);
}
}
int main()
{
int N = 4, K = 8;
check(K, N);
cout << countNums << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int countNums = 0 ;
static void checkUtil( int num, int K, int N)
{
if (N == 1 ) {
countNums++;
return ;
}
if ((num % 10 + K) <= 9 )
checkUtil( 10 * num +
(num % 10 + K), K, N - 1 );
if (K> 0 ) {
if ((num % 10 - K) >= 0 )
checkUtil( 10 * num +
num % 10 - K, K, N - 1 );
}
}
static void check( int K, int N)
{
for ( int i = 1 ; i <= 9 ; i++) {
checkUtil(i, K, N);
}
}
public static void main(String[] args)
{
int N = 4 , K = 8 ;
check(K, N);
System.out.print(countNums + "\n" );
}
}
|
Python3
countNums = 0 ;
def checkUtil(num, K, N):
global countNums;
if (N = = 1 ):
countNums + = 1 ;
return ;
if ((num % 10 + K) < = 9 ):
checkUtil( 10 * num + (num % 10 + K), K, N - 1 );
if (K > 0 ):
if ((num % 10 - K) > = 0 ):
checkUtil( 10 * num + num % 10 - K, K, N - 1 );
def check(K, N):
for i in range ( 1 , 10 ):
checkUtil(i, K, N);
if __name__ = = '__main__' :
N = 4 ;
K = 8 ;
check(K, N);
print (countNums);
|
C#
using System;
class GFG{
static int countNums = 0;
static void checkUtil( int num, int K, int N)
{
if (N == 1)
{
countNums++;
return ;
}
if ((num % 10 + K) <= 9)
checkUtil(10 * num +
(num % 10 + K), K, N - 1);
if (K > 0)
{
if ((num % 10 - K) >= 0)
checkUtil(10 * num +
num % 10 - K, K, N - 1);
}
}
static void check( int K, int N)
{
for ( int i = 1; i <= 9; i++)
{
checkUtil(i, K, N);
}
}
public static void Main(String[] args)
{
int N = 4, K = 8;
check(K, N);
Console.Write(countNums + "\n" );
}
}
|
Javascript
<script>
let countNums = 0;
function checkUtil(num, K, N)
{
if (N == 1) {
countNums++;
return ;
}
if ((num % 10 + K) <= 9)
checkUtil(10 * num +
(num % 10 + K), K, N - 1);
if (K) {
if ((num % 10 - K) >= 0)
checkUtil(10 * num +
num % 10 - K, K, N - 1);
}
}
function check(K, N)
{
for (let i = 1; i <= 9; i++) {
checkUtil(i, K, N);
}
}
let N = 4, K = 8;
check(K, N);
document.write(countNums + '<br>');
</script>
|
Time Complexity: O(2N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...