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 | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
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.