loop from the end of given number till you get a digit less than the
previously scanned digit.Let the index of that number be 'i' .

if index = -1,then the given number is the largest one
else do the following
1) swap the digit at the index i with the digit just greater than it in the
scanned portion
2) sort the remaining scanned digits after index i.

Ex:-  let the given number be 51432
the digit '1' is the first digit less that its previously scanned digit '4'
thus, we swap 2 which is the smallest greater digit the '1' in scanned
portion to get 52431 and the we sort the remaining digits after the index
to get 52134.

-- 


Reply via email to