D
D
dmitriyivvvv2018-06-26 17:12:26
Algorithms
dmitriyivvvv, 2018-06-26 17:12:26

Does such an algorithm exist?

Good afternoon! Is there such an algorithm for finding the nearest larger number from the digits of the original number. For example, given the original number 7809, its nearest greater will be 7890. So, does such an algorithm exist or will you have to perform all possible permutations of digits in this number and save those that are larger than the original one, in order to sort and display the smallest one?
For example, the number 47651270590822 is the next largest of the digits of the original number 47651270592028
can you describe in steps?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
D
dmitriyivvvv, 2018-06-26
@dmitriyivvvv

The answer is found if someone is interested, then here it is: we
move from right to left in search of a number that will be less than the previous one. For example, for 534976 it will be 4. Then to the right of this number we look for the smallest number that will be greater than this number (> 4) in this case it is 6> 4. Swap them. We get 536974, then sort in ascending order of the number after 6 (in this case 974 => 479) we get the desired number 536479

L
Lander, 2018-06-26
@usdglander

Think logically.
The nearest larger may differ in the lower digits. Therefore, the algorithm is as follows:
1. n = 2
2. Take n least significant digits and rearrange them.
3. Compare with the initial one, if the result is not achieved, then n++ and p.2.
4. Number found.

S
Sergey Sokolov, 2018-06-26
@sergiks

Upd. The algorithm below is incorrect, because does not work when it is necessary to rearrange several digits at once.
We consider pairs of unequal digits in a number.
By interchanging them, you can increase the number only when the left digit is less than the right. 23 -> 32
The size of the increase is smaller, the more to the right both numbers are. They can go not in a row, but, say, through one.
The coefficient to be minimized is a decimal number of zeros with ones in those positions. Let's say it's better to swap positions 2 and 3 than 4 and 1, because 110 < 1001Those. when iterating over positions, we least of all want to move the left position until we have gone through all the options for the right one:

Options for 4
0011
0101
0110
1001
1010
1100

Hence the algorithm .
  1. from right to left, iterate over pairs of digits until the first pair is found, where the
    left is less than the right
    Everything.

S
Sergey, 2018-06-26
@SuNbka

Sort ascending/descending? after which the previous and next numbers near X (7809) are taken and compared.

U
uvelichitel, 2018-06-26
@uvelichitel

Considering a number as an array of digits
1 We are looking for a digit less than the last (when comparing, you can do permutations, for example, as in bubble sort)
2. Rearrange the found digit with a bubble. 3. Sort the tail from the found position
(for example, by the same bubble sort for the simplicity of the algorithm )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question