N digit numbers having difference between the first and last digits as K | Function to store and check the difference of digits ; Base Case ; Last digit of the number to check the difference from the first digit ; Condition to avoid repeated values ; Update the string pt ; Recursive Call ; Update the string pt ; Recursive Call ; Any number can come in between first and last except the zero ; Recursive Call ; Function to place digit of the number ; 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 ; This loop place the digit at the starting ; Recursive Call ; Vector to store results ; Generate all the resultant number ; Print the result ; Driver Code ; Function Call
result = [ ] NEW_LINE def findNumbers ( st , prev , n , K ) : NEW_LINE INDENT global result NEW_LINE if ( len ( st ) == n ) : NEW_LINE INDENT result . append ( int ( st ) ) NEW_LINE return NEW_LINE DEDENT if ( len ( st ) == n - 1 ) : NEW_LINE INDENT if ( prev - K >= 0 ) : NEW_LINE INDENT pt = " " NEW_LINE pt += prev - K + 48 NEW_LINE findNumbers ( st + pt , prev - K , n , K ) NEW_LINE DEDENT if ( K != 0 and prev + K < 10 ) : NEW_LINE INDENT pt = " " NEW_LINE pt += prev + K + 48 NEW_LINE findNumbers ( st + pt , prev + K , n , K ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT for j in range ( 1 , 10 , 1 ) : NEW_LINE INDENT pt = " " NEW_LINE pt += j + 48 NEW_LINE findNumbers ( st + pt , prev , n , K ) NEW_LINE DEDENT DEDENT DEDENT def numDifference ( N , K ) : NEW_LINE INDENT global result NEW_LINE st = " " NEW_LINE if ( N == 1 and K == 0 ) : NEW_LINE INDENT result . append ( 0 ) NEW_LINE DEDENT elif ( N == 1 and K > 0 ) : NEW_LINE INDENT return result NEW_LINE DEDENT for i in range ( 1 , 10 , 1 ) : NEW_LINE INDENT temp = " " NEW_LINE temp += str ( 48 + i ) NEW_LINE findNumbers ( st + temp , i , N , K ) NEW_LINE st = " " NEW_LINE DEDENT return result NEW_LINE DEDENT def numDifferenceUtil ( N , K ) : NEW_LINE INDENT res = [ ] NEW_LINE res = numDifference ( N , K ) NEW_LINE for i in range ( 1 , len ( res ) ) : NEW_LINE INDENT print ( res [ i ] + 40 , end = " ▁ " ) NEW_LINE break NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 2 NEW_LINE K = 9 NEW_LINE numDifferenceUtil ( N , K ) NEW_LINE DEDENT