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.
Input: N = 2, K = 0
Output: 11, 22, 33, 44, 55, 66, 77, 88, 99
Input: N = 2, K = 9
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:
Time Complexity: O(2N)
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.
- Count numbers in a range with digit sum divisible by K having first and last digit different
- 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
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- 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 non-palindromic array elements having same first and last digit
- Generate all N digit numbers having absolute difference as K between adjacent digits
- 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 n digit numbers not having a particular digit
- Count numbers with same first and last 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
- Count numbers formed by given two digit with sum having given digits
- Count of N digit Numbers having no pair of equal consecutive Digits
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
Improved By : bgangwar59