Created
February 4, 2013 03:08
-
-
Save oschrenk/4704817 to your computer and use it in GitHub Desktop.
Given a number, find the next higher number which has the exact same set of digits as the original number. For example: given 38276 return 38627
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.acme.numbers; | |
import java.util.Arrays; | |
/** | |
* | |
* Given a number, find the next higher number which has the exact same set of | |
* digits as the original number. For example: given 38276 return 38627 | |
* | |
* | |
*/ | |
public class NextHighestLongSameDigits { | |
/** | |
* Starting from the right, you find the first pair-of-digits such that the | |
* left-digit is smaller than the right-digit. Let's refer to the left-digit | |
* by "digit-x". Find the smallest number larger than digit-x to the right | |
* of digit-x, and place it immediately left of digit-x. Finally, sort the | |
* remaining digits in ascending order - since they were already in | |
* descending order, all you need to do is reverse them (save for digit-x, | |
* which can be placed in the correct place in O(n)). | |
* | |
*/ | |
public long find(long number) { | |
final String s = String.valueOf(number); | |
int min = -1; | |
int minPos = -1; | |
for (int i = 1; i < s.length(); i++) { | |
int right = Character.getNumericValue(s.charAt(i)); | |
int left = Character.getNumericValue(s.charAt(i - 1)); | |
if (right < left) { | |
minPos = i; | |
min = right; | |
break; | |
} | |
} | |
if (minPos == -1) { | |
throw new IllegalArgumentException("No possible solution available"); | |
} | |
int minNext = 9; | |
int minNextPos = -1; | |
for (int i = minPos + 1; i < s.length(); i++) { | |
int current = Character.getNumericValue(s.charAt(i)); | |
if (current > min && current <= minNext) { | |
minNext = current; | |
minNextPos = i; | |
} | |
} | |
StringBuilder sb = new StringBuilder(s); | |
sb.setCharAt(minPos, Character.forDigit(minNext, 10)); | |
sb.setCharAt(minNextPos, Character.forDigit(min, 10)); | |
char[] charArray = sb.substring(minPos + 1).toCharArray(); | |
Arrays.sort(charArray); | |
sb.replace(minPos + 1, s.length(), new String(charArray)); | |
return Long.valueOf(sb.toString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
for input 1234675 this would throw StringIndexOutOfBoundsException at line 56.