Posts

Showing posts from May, 2017

Find next greater number with same set of digits!!

// C++ program to find the smallest number which greater than a given number // and has same set of digits as given number #include <iostream> #include <cstring> #include <algorithm> using namespace std; // Utility function to swap two digits void swap(char *a, char *b) { char temp = *a; *a = *b; *b = temp; } // Given a number as a char array number[], this function finds the // next greater number. It modifies the same array to store the result void findNext(char number[], int n) { int i, j; // I) Start from the right most digit and find the first digit that is // smaller than the digit next to it. for (i = n-1; i > 0; i--) if (number[i] > number[i-1]) break; // If no such digit is found, then all digits are in descending order // means there cannot be a greater number with same set of digits if (i==0) { cout << "Next number is not possible"; return; } // II) Find the smallest digit on rig...