Find next greater number with same set of digits C#
#include
#include
#include
using namespace std;
void swap(char *a, char *b){
char temp = *a;
*a = *b;
*b = temp;
}
void findNextNumber(char num[], int n)
{
int i, j;
for (i = n-1; i > 0; i--)
if (num[i] > num[i-1])
break;
if (i==0)
{
cout << "Next number is not possible";
return;
}
int x = num[i-1], smallest = i;
for (j = i+1; j < n; j++) if (num[j] > x && num[j] < num[smallest])
smallest = j;
swap(&num[smallest], &num[i-1]);
sort(num + i, num + n);
cout << "Next number with same set of digits is " << num;
return;
}
// Driver program to test above function
int main(){
char digits[] = "534976";
int len = strlen(digits);
findNextNumber(digits, len);
return 0;
}
Output:
Next number with same set of digits is 536479