Jump to content

Next highest int from digits: Difference between revisions

m
used gold section header for E.g., added whitespace, added whitespace before the TOC, corrected a misspelling.
m (used gold section header for E.g., added whitespace, added whitespace before the TOC, corrected a misspelling.)
Line 4:
integer using only the given digits<sup>*1</sup>.
 
* &nbsp; Numbers will not be padded to the left with zeroes.
* &nbsp; Use all given digits, with their given multiplicity. (If a digit appears twice in the input number, it should appear twice in the result).
* &nbsp; If there is no next higesthighest integer return zero.
 
<br><sup>*1</sup> &nbsp; Alternatively phrased as: "Find the smallest integer larger than the (positive or zero) integer N which can be obtained by reordering the (base 10) digits of N".
 
<br><sup>*1</sup> Alternatively phrased as: "Find the smallest integer larger than the (positive or zero) integer N which can be obtained by reordering the (base 10) digits of N".
 
;Algorithm 1:
# &nbsp; Generate all the permutations of the digits and sort into numeric order.
# &nbsp; Find the number in the list.
# &nbsp; Return the next highest number from the list.
 
 
The above could prove slow and memory hungry for numbers with large numbers of
Line 20 ⟶ 22:
 
;Algorithm 2:
# &nbsp; Scan right-to-left through the digits of the number until you find a digit with a larger digit somewhere to the right of it.
# &nbsp; Exchange that digit with the digit on the right that is ''both'' more than it, and closest to it.
# &nbsp; Order the digits to the right of this position, after the swap; lowest-to-highest, left-to-right. (I.e. so they form the lowest numerical representation)
 
 
E.g:
'''E.g.:'''
<pre>
n = 12453
Line 38 ⟶ 41:
 
This second algorithm is faster and more memory efficient, but implementations
may be harder to test. One method of testing ,(as used in developing the task),
 
is to compare results from both algorithms for random numbers generated from a
One method of testing, (as used in developing the task), &nbsp; is to compare results from both
algorithms for random numbers generated from a range that the first algorithm can handle.
 
 
;Task requirements:
Calculate the next highest int from the digits of the following numbers:
:* &nbsp; 0
:* &nbsp; 9
:* &nbsp; 12
:* &nbsp; 21
:* &nbsp; 12453
:* &nbsp; 738440
:* &nbsp; 45072010
:* &nbsp; 95322020
 
 
;Optional stretch goal:
:* &nbsp; 9589776899767587796600
<br><br>
 
=={{header|C}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.