Skip to content

Instantly share code, notes, and snippets.

@kopiro
Created February 25, 2016 20:49
Show Gist options
  • Save kopiro/4c1f342665b5c3b1f45a to your computer and use it in GitHub Desktop.
Save kopiro/4c1f342665b5c3b1f45a to your computer and use it in GitHub Desktop.
Next bigger number with the same digits
function nextBigger(n){
var d = n.toString().split('');
// find the pivot, the point (from right) where i > i-1
var p = -1;
for (var i = d.length-1; i > 0; i--) {
if (+d[i] > +d[i-1]) {
p = i-1;
break;
}
}
// if we are unable to find the pivot, skip
if (p == -1) return p;
// splice the digits in the pivot
var right = d.splice(p);
// extract pivot
var pv = right.splice(0, 1)[0];
// find the lowest number > pv
var mm = null, mmi = null;
for (var i = 0; i < right.length; i++) {
if (right[i] > pv) {
if (mm == null || right[i] < mm) {
mm = right[i];
mmi = i;
}
}
}
if (mmi == null) return -1;
right.splice(mmi, 1);
right.push(pv);
right = right.sort();
// concat the left + new pivot + right part
var ret = +d.concat([mm]).concat(right).join('');
if (ret < n) return -1;
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment