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:
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.
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Next Number with distinct digits
- Find smallest number with given number of digits and sum of digits under given constraints
- Numbers having Unique (or Distinct) digits
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Find next greater number with same set of digits
- Minimum digits to be removed to make either all digits or alternating digits same
- Find the Largest number with given number of digits and sum of digits
- Next higher palindromic number using the same set of digits
- Next greater Number than N with the same quantity of digits A and B
- Find the average of k digits from the beginning and l digits from the end of the given number
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Print a number strictly less than a given number such that all its digits are distinct.
- Count numbers formed by given two digit with sum having given digits
- Number of digits in the nth number made of given four digits
- Find the String having each substring with exactly K distinct characters
- Find the number in a range having maximum product of the digits
- Sum of the digits of square of the given number which has only 1's as its digits
- Rearrange Array to maximize number having Array elements as digits based on given conditions
- Given a number, find the next smallest palindrome
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.