[Codewars] Next bigger number with the same digits

Next bigger number with the same digits

  • Time :
  • Space :
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;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/05/01/PS/Codewars/next-bigger-number-with-the-same-digits/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.