This is a variation of "Next Permutation Problem".

- convert integer n to string s.
- find the increasing sequence from right to left, use i to denote the position of first decreasing digit. If i<0, no larger number, return -1;
- find the first number that is larger than s[i], use j to denote the position.
- swap s[i] and s[j].
- reverse the sub string from s[i+1] to the end;
- check the result if it is over the integer's upper limit.

```
int nextGreaterElement(int n) {
string s = to_string(n);
if (s.size()==1) return -1;
int i =s.size()-2;
while (i>=0 && s[i]>=s[i+1]) i--;
if (i<0) return -1;
int j = s.size()-1;
while (j>i && s[j]<=s[i]) j--;
swap(s[i], s[j]);
reverse(s.begin()+i+1, s.end());
long res = stol(s);
return res > 0x7FFFFFFF? -1:(int)res;
}
```