```
void nextPermutation(vector<int> &num) {
for(int i = num.size() - 2; i >= 0; i--){
if(num[i] < num[i + 1]){
int pos;
int diff = INT_MAX;
for(int j = i + 1; j < num.size(); j++){
if(num[j] > num[i] && abs(num[i] - num[j]) < diff){
diff = abs(num[i] - num[j]);
pos = j;
}
}
swap(num[i], num[pos]);
sort(num.begin() + i + 1, num.end());
return;
}
}
sort(num.begin(), num.end());
}
```

For this problem, coding is not a big deal. Algorithm is!

Now let's pick a number, for example, 24387651.

what is the next permutation? 24513678.

How can I get the answer?

First step: find the first ascending digit from the back of the number. 3 < 8 > 7 > 6 > 5 > 1. Then 3 is the digit.

Second step: swap that digit with the next big digit in following digits. Which one is the next big digit in 87651? 5! So swap them. Now the number becomes 24587631.

Third step: sort 87631 into 13678. The final answer is 24513678.