how to find the next number which has all different digits and greater than the given number N? Like if N=2000 then the answer is 2013. how to approach this problem. where 1<=N<=10^16
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 166 |
2 | maomao90 | 163 |
2 | Um_nik | 163 |
4 | atcoder_official | 161 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | nor | 153 |
9 | Dominater069 | 153 |
how to find the next number which has all different digits and greater than the given number N? Like if N=2000 then the answer is 2013. how to approach this problem. where 1<=N<=10^16
Name |
---|
The answer will not exits if it has more than 10 digits. for less than or equal to 10 digits you can form all the permutations.
How permutations give answer?
Explaining how will be spoon-feeding.
If you code in Python, you could use a dictionary that keeps adding the value TRUE for every digit of the number in consideration. After all the digits of the number have been traversed AND if a digit is found to already have a value TRUE in dictionary, clear the dictionary and move on to the next number. When a number with all digits distinct is found, break out of the loop.
I think that's working upto 10^8 ,but my need is upto 10^16.
But how can it be upto 10^16 ? Digits will starting repeating in every number after 9876543210. :/
Could you please post a link to this problem? Thanks!
The question link u posted doesnt have such big limits on n..
Here's my greedy approach sorry abit messy that I believe will work for any number. The idea is:
ans cannot exceed 9876543210
start from left to right and when you find the digit that occurred before increase it, if it's 9 in increasing it will genereate a carry which which we have to add toward left.