Last active
August 29, 2015 14:27
-
-
Save legends2k/7109138ee67c8852bf78 to your computer and use it in GitHub Desktop.
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
| // find the next larger integer from the given number's digits | |
| #include <iostream> | |
| #include <cstring> | |
| int main(int argc, char *argv[]) { | |
| if (argc < 2) { | |
| std::cout << "Usage: nexti <integer>\n"; | |
| return 0; | |
| } | |
| char *str = argv[1]; | |
| int const last = strlen(str) - 1; | |
| auto i = last; | |
| // find the pivot, the number smaller than the previous, going from LSD towards MSD | |
| // e.g. for 132959642 we'd stop at 9 i.e. just before 5, the pivot | |
| while ((i > 0) && (str[i - 1] >= str[i])) --i; | |
| if (i == 0) { | |
| std::cout << "No larger integer may be formed with given digits.\n"; | |
| return 0; | |
| } | |
| // substring to be actually fixed; everything before pivot is in tact | |
| // in our example, str would be pointing to 5 now | |
| str += i - 1; | |
| // this substring is verified to be in descending order, except the MSD | |
| // reverse it excluding the MSD | |
| for (auto c = 0; c < ((last - i + 1) / 2); ++c) | |
| std::swap(argv[1][i + c], argv[1][last - c]); | |
| // find the next number larger than the pivot | |
| // in our example, we'd now have 132952469, find 6 and swap it with 5 | |
| i = 1; | |
| while (str[i] <= *str) ++i; | |
| // swap that with the pivot | |
| std::swap(*str, str[i]); | |
| std::cout << argv[1]; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment