Next bigger number with the same digits
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #include <bits/stdc++.h> using namespace std; vector<int> build(long n) { vector<int> res; while(n) { res.push_back(n % 10); n /= 10; } reverse(begin(res), end(res)); return res; } long nextBigger(long n) { vector<int> perm = build(n); next_permutation(begin(perm), end(perm)); long res = 0; for(int i = 0; i < perm.size(); i++) res = res * 10 + perm[i]; return res > n ? res : -1; }
|