class Solution { public: int nextGreaterElement(int n) { string s=to_string(n); next_permutation(s.begin(),s.end()); if(stoll(s)>n&&stoll(s)<=INT_MAX) { return stoll(s); } return -1; } };
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter