Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ConsecutiveSolution {
- public static int consecutive(int n) {
- String digs = Integer.toString(n);
- for (int i = 1; i < digs.length(); i++) {
- if (digs.charAt(i) == digs.charAt(i - 1)) {
- return i;
- }
- }
- return -1;
- }
- public static int solution(int n) {
- int culprit = consecutive(n);
- if (culprit >= 1) {
- char[] digs = Integer.toString(n).toCharArray();
- if (digs[culprit] <= '8') {
- digs[culprit] += 1;
- int dig = 0;
- for (int i = culprit + 1; i < digs.length; i++) {
- digs[i] = (char) (dig + '0');
- dig = (dig + 1) % 2;
- }
- return Integer.parseInt(new String(digs));
- } else {
- return solution(n + (int) Math.pow(10, digs.length - culprit - 1));
- }
- } else {
- culprit = consecutive(n + 1);
- return culprit == -1 ? n + 1 : solution(n + 1);
- }
- }
- public static void main(String[] args) {
- int n = 12345; // Your input number
- int result = solution(n);
- System.out.println(result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement