Runaround numbers are integers with unique digits, none of which is zero (e.g., 81362) that also have an interesting property, exemplified by this demonstration:
Given a number M (that has anywhere from 1 through 9 digits), find and print the next runaround number higher than M, which will always fit into an unsigned long integer for the given test data.
A single line with a single integer, M
81361
A single line containing the next runaround number higher than the input value, M.
81362
public class runround {
public static void main(String[] args) throws IOException {
try (final BufferedReader f = new BufferedReader(new FileReader("runround.in"));
final PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("runround.out")))) {
final int M = Integer.parseInt(f.readLine());
for (int m = M + 1; m < 999999999; m++) {
final String number = Integer.toString(m);
if (isRunround(number)) {
out.println(m);
break;
}
}
}
}
private static boolean isRunround(String number) {
boolean[] seen = new boolean[10];
boolean[] visitedIndex = new boolean[number.length() + 1];
int index = 0;
int increment = Character.getNumericValue(number.charAt(index));
int count = 0;
do {
// 0 is an invalid increment
if (increment == 0) {
return false;
}
// check if number wraps around to itself
if (increment % number.length() == 0) {
return false;
}
// check if increment is repeated
if (seen[increment]) {
return false;
}
seen[increment] = true;
// advance index
index = (index + increment) % (number.length());
// check if next index has been visited before
if (visitedIndex[index]) {
return false;
}
visitedIndex[index] = true;
increment = Character.getNumericValue(number.charAt(index));
} while (++count < number.length());
return true;
}
}
Link To: Java Source Code