Given a natural number N, the task is to find the next number having distinct digits from the given number.
Input: N = 19
Next number to 19 whose digits are different from 19 is 20.
Input: N = 2019
Next number to 2019 whose digits are different from 2019 is 3333.
Approach: The idea is to use hashing to compute the next number with distinct digit from the given number –
- Create a hash array to store the digits present in the number, store the most significant digit and also store the number of digits present in the number in a count variable
- Find the next digit for the most significant bit which is not present in the number and greater than the current most significant digit.
- If the next most significant digit is not found then increase the number of digits by incrementing the count and find the most significant digit between 1 to 9 which is not present in the number.
- If the next most significant digit is found then find the minimum digit next which is not present in the number between 0 to 9.
- Iterate over the number of digits from 1 to count
- Multiply the most significant digit by 10 and add the next digit which is not present in the given number.
Below is the implementation of the above approach:
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Print a number strictly less than a given number such that all its digits are distinct.
- Next Number with distinct digits
- Find smallest number with given number of digits and sum of digits under given constraints
- Find the Largest number with given number of digits and sum of digits
- Find smallest possible Number from a given large Number with same count of digits
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Find the smallest number whose digits multiply to a given number n
- Find count of digits in a number that divide the number
- Find maximum number that can be formed using digits of a given number
- Find M-th number whose repeated sum of digits of a number is N
- Find the average of k digits from the beginning and l digits from the end of the given number
- Queries to find whether a number has exactly four distinct factors or not
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Find first and last digits of a number
- Given a number n, find the first k digits of n^n
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.