Digit arrangement

Try this simple algorithm on for size.

Design an algorithm that, when given an arrangement of the digits 0,1,2,3,4,5,6,7,8,9, rearranges the digits so that the new arrangement represents the next larger value that can be represented by these digits (or reports that no such rearrangement exists if no rearrangement produces a larger value). Thus 5647382901 would produce 5647382910.
LVL 3
TheTechGuysNYCAsked:
Who is Participating?
 
awking00Connect With a Mentor Commented:
In pseudo code -
Add 9 to the number to get a new number
If the sum of the digits of the new number does not = 45
then continue adding 9 until they do.
0
 
TheTechGuysNYCAuthor Commented:
Hint:

A few examples to help the process...

0123456789
0123456798
0123456897
0123456879

0
 
TheTechGuysNYCAuthor Commented:
Psuedocode is all that is needed to be correct...
0
Worried about phishing attacks?

90% of attacks start with a phish. It’s critical that IT admins and MSSPs have the right security in place to protect their end users from these phishing attacks. Check out our latest feature brief for tips and tricks to keep your employees off a hackers line!

 
ozoConnect With a Mentor Commented:
#!/usr/bin/perl
sub permute {
    my ($hold, @items) = @_;
    print("@items\n"), return if $hold+1==@items;
    foreach $i ($hold..$#items) {
      @items[$hold, $i] = @items[$i, $hold];
      permute($hold+1, @items);
    }
}
@arr=(0..9);
permute(0, @arr);
0
 
awking00Commented:
Are you sure you don't mean for your examples to be like this?
0123456789
0123456798
0123456879
0123456897
0
 
ozoCommented:
adding 9 until the sum = 45 will get you to 123456888, which is not a rearangement
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.