Skip to content

Instantly share code, notes, and snippets.

@rohith2506
Created March 3, 2015 08:50
Show Gist options
  • Select an option

  • Save rohith2506/2b8454590be41abd49ce to your computer and use it in GitHub Desktop.

Select an option

Save rohith2506/2b8454590be41abd49ce to your computer and use it in GitHub Desktop.
Next greatest number
// 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