Given a number N, the task is to find the Nth number which has an absolute difference of 1 between every pair of its adjacent digits.
Examples:
Input : N = 5
Output : 5
Explanation:
The first 5 such numbers are 1,2,3,4 and 5.Input : N = 15
Output : 23
Explanation:
The first 15 such numbers are 1,2,3,4,5,6,7,8,9,10,11,12,21,22 and 23.
Approach: In order to solve this problem we are using the Queue data structure.
- Prepare an empty Queue, and Enqueue all integers 1 to 9 in increasing order.
- Now perform the following operation N times.
- Dequeue and store in array arr which stores ith number of required type in arr[i].
- If (arr[i] % 10 != 0), then enqueue 10 * arr[i] + (arr[i] % 10) – 1.
- Enqueue 10 * arr[i] + (arr[i] % 10).
- If (arr[i] % 10 != 9), then enqueue 10 * arr[i] + (arr[i] % 10) + 1.
- Return arr[N] as the answer.
Below is the implementation of the given approach:
C++
// C++ Program to find Nth number with // absolute difference between all // adjacent digits at most 1. #include <bits/stdc++.h> using namespace std; // Return Nth number with // absolute difference between all // adjacent digits at most 1. void findNthNumber(int N) { // To store all such numbers long long arr[N + 1]; queue<long long> q; // Enqueue all integers from 1 to 9 // in increasing order. for (int i = 1; i <= 9; i++) q.push(i); // Perform the operation N times so that // we can get all such N numbers. for (int i = 1; i <= N; i++) { // Store the front element of queue, // in array and pop it from queue. arr[i] = q.front(); q.pop(); // If the last digit of dequeued integer is // not 0, then enqueue the next such number. if (arr[i] % 10 != 0) q.push(arr[i] * 10 + arr[i] % 10 - 1); // Enqueue the next such number q.push(arr[i] * 10 + arr[i] % 10); // If the last digit of dequeued integer is // not 9, then enqueue the next such number. if (arr[i] % 10 != 9) q.push(arr[i] * 10 + arr[i] % 10 + 1); } cout<<arr[N]<<endl; } // Driver Code int main() { int N = 21; findNthNumber(N); return 0; } |
Java
// Java program to find Nth number with // absolute difference between all // adjacent digits at most 1. import java.util.*; class GFG{ // Return Nth number with // absolute difference between all // adjacent digits at most 1. static void findNthNumber(int N) { // To store all such numbers int []arr = new int[N + 1]; Queue<Integer> q = new LinkedList<>(); // Enqueue all integers from 1 to 9 // in increasing order. for(int i = 1; i <= 9; i++) q.add(i); // Perform the operation N times so // that we can get all such N numbers. for(int i = 1; i <= N; i++) { // Store the front element of queue, // in array and pop it from queue. arr[i] = q.peek(); q.remove(); // If the last digit of dequeued // integer is not 0, then enqueue // the next such number. if (arr[i] % 10 != 0) q.add(arr[i] * 10 + arr[i] % 10 - 1); // Enqueue the next such number q.add(arr[i] * 10 + arr[i] % 10); // If the last digit of dequeued // integer is not 9, then enqueue // the next such number. if (arr[i] % 10 != 9) q.add(arr[i] * 10 + arr[i] % 10 + 1); } System.out.println(arr[N]); } // Driver Code public static void main(String[] args) { int N = 21; findNthNumber(N); } } // This code is contributed by Amit Katiyar |
Python3
# Python 3 Program to find Nth number with # absolute difference between all # adjacent digits at most 1. # Return Nth number with # absolute difference between all # adjacent digits at most 1. def findNthNumber(N): # To store all such numbers arr = [0 for i in range(N + 1)] q = [] # Enqueue all integers from 1 to 9 # in increasing order. for i in range(1, 10, 1): q.append(i) # Perform the operation N times so that # we can get all such N numbers. for i in range(1, N+1, 1): # Store the front element of queue, # in array and pop it from queue. arr[i] = q[0] q.remove(q[0]) # If the last digit of dequeued integer is # not 0, then enqueue the next such number. if (arr[i] % 10 != 0): q.append(arr[i] * 10 + arr[i] % 10 - 1) # Enqueue the next such number q.append(arr[i] * 10 + arr[i] % 10) # If the last digit of dequeued integer is # not 9, then enqueue the next such number. if (arr[i] % 10 != 9): q.append(arr[i] * 10 + arr[i] % 10 + 1) print(arr[N]) # Driver Code if __name__ == '__main__': N = 21 findNthNumber(N) # This code is contributed by Samarth |
45
Recommended Posts:
- Count of numbers upto N having absolute difference of at most K between any two adjacent digits
- Count of N-digit numbers with absolute difference of adjacent digits not exceeding K
- Count of N-digit numbers with absolute difference of adjacent digits not exceeding K | Set 2
- Missing occurrences of a number in an array such that maximum absolute difference of adjacent elements is minimum
- Maximise sum of absolute difference between adjacent elements in Array with sum K
- Sort an Array based on the absolute difference of adjacent elements
- Minimize the maximum absolute difference of adjacent elements in a circular array
- Absolute difference between the first X and last X Digits of N
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Find the smallest positive number which can not be represented by given digits
- Sort elements of an array in increasing order of absolute difference of adjacent elements
- Number of possible permutations when absolute difference between number of elements to the right and left are given
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Minimum absolute difference of a number and its closest prime
- Queries to return the absolute difference between L-th smallest number and the R-th smallest number
- Print first k digits of 1/n where n is a positive integer
- Minimum Possible sum of digits in a positive multiple of N
- Count of digits after concatenation of first N positive integers
- Count positive integers with 0 as a digit and maximum 'd' 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 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.


