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
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
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
Название |
---|
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.