Created
March 3, 2015 08:50
-
-
Save rohith2506/2b8454590be41abd49ce to your computer and use it in GitHub Desktop.
Next greatest number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For eg: 396 --> i=0 => and j = 2 swap(3,6) ==> 693 ==> sort(1..n) => 6 [93] => 639 (this is the required number) | |
| int ngd(n){ | |
| if number is sorted in ascedning order just swap last two digits | |
| if number is sorted in descending order impossible | |
| else { | |
| find the condition a[j] > a[j-1] from last and stop there | |
| let n1 = a[j] | |
| find next smallest greater digit than n1 | |
| let n2 = a[i] | |
| swap(a[j], a[i]) | |
| then sort(a[j+1..n]) | |
| this is the next greatest number | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment