Let the number is stored in an array a[n] with MSB at index 0...
1. i = n-1;
   reduce  i  till a[i]<=a[i-1] & i > 0.

If here i =0 means give number is largest possible number made out of digits
otherwise i is pointing to a digit such that a[i]<a[i+1]

2. find smallest digit from a[i+1 to n-1] just larger than a[i].
   lets assume it come out as a digit of index j.
3. swap a[i] and a[j].
Finally sort a[i+1 to n-1] in increasing order...

suppose a=5436782
so
after step 1, i=4
after step 2, j=5
after step 4, a=5436872
finally after sorting  a=5436827


----------------------------------------------------------------------------------------------------------------------
Ram Chandra Sankhala | 3rd Year  | Computer Science & Engineering | IIT Roorkee

-- 


Reply via email to