To solve the problem of finding the smallest integer greater than a given integer N that does not contain two identical consecutive digits, we can follow a systematic approach. Below, I'll outline an efficient algorithm and provide a Java implementation that adheres to the problem constraints.
Problem Breakdown
Given an integer N, we need to find the smallest integer M such that:
M > N
M does not contain two identical consecutive digits.
Examples
-
Input: N = 55
Output: 56
-
Input: N = 1765
Output: 1767
-
Input: N = 98
Output: 101
-
Input: N = 44432
Output: 45010
-
Input: N = 3298
Output: 3401
Algorithm Overview
A straightforward approach is to incrementally check each number greater than N until we find one that satisfies the condition. However, for large values of N, this can be inefficient.
To optimize, we can:
-
Convert N to a String or an array of digits: This allows us to easily manipulate individual digits.
-
Iterate through the digits from left to right:
- At each digit, ensure it is not the same as the previous digit.
- If a duplicate is found, increment the previous digit and set all following digits to the smallest possible digits (starting from
0) while ensuring no consecutive duplicates.
-
Handle Edge Cases:
- Cases where incrementing a digit causes a carry-over (e.g., from
9 to 10).
- Ensuring the new number doesn't introduce new consecutive duplicates after manipulation.
-
Convert the modified digits back to an integer:
- Once the digits are adjusted to meet the criteria, convert them back to an integer to obtain
M.
However, given that the maximum value of N is within the range of a 32-bit signed integer in Java (2,147,483,647), the incremental approach is feasible and efficient enough for this problem.
Java Implementation
Below is the Java implementation of the solution. The approach involves incrementing N by 1 until a number without consecutive duplicate digits is found.
public class Solution {
public int solution(int N) {
int M = N + 1;
while (hasConsecutiveDuplicates(M)) {
M++;
// To prevent potential integer overflow
if (M < 0) {
throw new IllegalArgumentException("No valid number found within integer range.");
}
}
return M;
}
private boolean hasConsecutiveDuplicates(int num) {
int lastDigit = -1;
while (num > 0) {
int currentDigit = num % 10;
if (currentDigit == lastDigit) {
return true;
}
lastDigit = currentDigit;
num /= 10;
}
return false;
}
// For testing purposes
public static void main(String[] args) {
Solution sol = new Solution();
int[] testCases = {55, 1765, 98, 44432, 3298};
int[] expectedResults = {56, 1767, 101, 45010, 3401};
for (int i = 0; i < testCases.length; i++) {
int result = sol.solution(testCases[i]);
System.out.println("Input: " + testCases[i] + " | Expected Output: " + expectedResults[i]
+ " | Actual Output: " + result
+ " | " + (result == expectedResults[i] ? "PASS" : "FAIL"));
}
}
}
Explanation of the Code
-
solution Method:
- Initializes
M to N + 1 since we need the smallest integer greater than N.
- Uses a
while loop to check if M has consecutive duplicate digits using the hasConsecutiveDuplicates method.
- If duplicates are found, increments
M by 1 and continues the loop.
- Includes a safety check to prevent integer overflow, throwing an exception if no valid number is found within the integer range.
-
hasConsecutiveDuplicates Method:
- Checks if a given number contains two identical consecutive digits.
- Iterates through each digit by taking the modulus with 10 and dividing by 10 to move to the next digit.
- Compares the current digit with the previous digit (
lastDigit). If they are the same, it returns true.
- If no duplicates are found after checking all digits, it returns
false.
-
main Method:
- Provides test cases to verify the correctness of the implementation.
- Compares the actual output with the expected results and prints whether each test case passes or fails.
Efficiency Considerations
-
Time Complexity: The worst-case time complexity is O(K * D), where K is the number of increments needed and D is the number of digits in the number. However, given the integer range, this is acceptable.
-
Space Complexity: O(1), as we only use a constant amount of extra space.
Potential Optimizations
While the incremental approach is efficient for the given constraints, for extremely large numbers or different constraints, more optimized methods (like backtracking or digit manipulation) could be employed to jump over large sequences of invalid numbers. However, such optimizations are unnecessary here.
Conclusion
The provided Java implementation effectively solves the problem by incrementally searching for the next valid number greater than N without two identical consecutive digits. The approach is both simple and efficient within the integer range constraints specified.