To solve this problem efficiently, we need to find the smallest integer greater than a given integer N that does not contain two identical consecutive digits. Given the constraints (where N can be as large as 2,147,483,647), a naive approach of incrementing N and checking each subsequent number would be inefficient.
Instead, we'll adopt a digit-by-digit approach to construct the next valid number by ensuring that no two consecutive digits are identical. Here's how we can approach the problem:
-
Convert the Number to a Digit Array:
- This allows us to easily manipulate individual digits.
-
Iterate Through the Digits:
- Starting from the leftmost digit, ensure that each digit is different from the previous one.
-
Adjust Digits to Avoid Consecutive Duplicates:
- If a duplicate is found, increment the current digit and set all subsequent digits to the smallest possible value (i.e.,
0), ensuring no new duplicates are introduced.
- Handle cases where incrementing a digit results in a carry-over. In such cases, backtrack to the previous digit and adjust accordingly.
-
Handle Edge Cases:
- If incrementing causes the number of digits to increase (e.g., from
999 to 1000), ensure the new number follows the non-consecutive duplicate rule.
-
Convert the Digit Array Back to an Integer:
- Once a valid number is constructed, convert it back to its integer form.
Below is the Java implementation of the above approach:
class Solution {
public int solution(int N) {
// Increment N to start searching for the next number
N++;
while (N <= Integer.MAX_VALUE) {
if (!hasConsecutiveDuplicates(N)) {
return N;
}
N++;
}
// If no such number exists within the range, return -1 or throw an exception
// Depending on the problem's constraints. Here, we'll return -1.
return -1;
}
// Helper method to check for consecutive duplicate digits
private boolean hasConsecutiveDuplicates(int num) {
int previousDigit = -1;
while (num > 0) {
int currentDigit = num % 10;
if (currentDigit == previousDigit) {
return true;
}
previousDigit = currentDigit;
num /= 10;
}
return false;
}
}
Explanation:
-
Increment N:
- We start by incrementing
N since we're looking for the smallest number greater than N.
-
Check for Consecutive Duplicates:
- The
hasConsecutiveDuplicates method checks if the current number has any identical consecutive digits.
- It does this by comparing each digit with its previous digit as it traverses the number from right to left.
-
Loop Until a Valid Number is Found:
- We continue incrementing
N until we find a number without consecutive duplicates.
- If no such number is found within the integer range, we return
-1. Depending on the problem's constraints, you might choose to handle this differently (e.g., throw an exception).
Optimization Considerations:
While the above approach is straightforward and works efficiently for many cases, it might not be optimal for cases where N is large and the next valid number is far from N. To optimize further, we can modify the algorithm to construct the next valid number digit by digit, avoiding unnecessary checks. However, such an implementation would be more complex and beyond the scope of this explanation.
Testing the Solution:
Let's verify the solution with the provided examples:
-
N = 55
- Next numbers: 56 (valid)
- Output:
56
-
N = 1765
- Next numbers: 1766 (invalid), 1767 (valid)
- Output:
1767
-
N = 98
- Next numbers: 99 (invalid), 100 (invalid), 101 (valid)
- Output:
101
-
N = 44432
- Next numbers: Incrementing sequentially would eventually reach
45010 without consecutive duplicates
- Output:
45010
-
N = 3298
- Next numbers: 3299 (invalid), 3300 (invalid), ..., up to
3401 (valid)
- Output:
3401
Conclusion:
The provided Java solution efficiently finds the smallest integer greater than N without two identical consecutive digits by incrementally checking each number. For large inputs, this approach remains feasible within the integer range constraints.