You are viewing a single comment's thread. Return to all comments →
Perl solution:
sub sorted_descending { my $arr = shift; for (my $i = 0; $i < scalar(@$arr) - 1; $i++) { return 0 if ($arr->[$i] lt $arr->[$i + 1]); } return 1; } sub biggerIsGreater { my $w = shift; my @arr = split("", $w); my $max = $arr[1]; my $ind = 0; my @res; if (sorted_descending(\@arr)) { return "no answer"; } else { my $k = $#arr - 1; while ($k >= 0 && $arr[$k] ge $arr[$k + 1]) { $k--; } my $l = $#arr; while ($arr[$l] le $arr[$k]) { $l--; } ($arr[$k], $arr[$l]) = ($arr[$l], $arr[$k]); push(@res, @arr[0..$k], reverse(splice(@arr, $k+1, $#arr))); return join("", @res); } }
Seems like cookies are disabled on this browser, please enable them to open this website
Bigger is Greater
You are viewing a single comment's thread. Return to all comments →
Perl solution: