Skip to content

Instantly share code, notes, and snippets.

@skdangi
Created November 8, 2013 19:55
Show Gist options
  • Save skdangi/7376672 to your computer and use it in GitHub Desktop.
Save skdangi/7376672 to your computer and use it in GitHub Desktop.
Solution for - find the next smallest palindrome larger than given number [http://www.geeksforgeeks.org/given-a-number-find-next-smallest-palindrome-larger-than-this-number/]
import java.util.Arrays;
public class PalindromeUtil {
final static char NINE = '9';
final static char ZERO = '0';
public static char[] nextPalindrome(char[] sourceNumber) {
if (null == sourceNumber || sourceNumber.length == 0) {
throw new IllegalArgumentException("Not valid value");
}
for (int itr = 0; itr < sourceNumber.length; itr++) {
if (sourceNumber[itr] < ZERO || sourceNumber[itr] > NINE) {
throw new IllegalArgumentException(
"Contains values other than digits");
}
}
int length = sourceNumber.length;
int start = 0;
int end = length - 1;
char[] target = Arrays.copyOf(sourceNumber, sourceNumber.length);
boolean alreadyPalindrome = true;
while (start < end) {
if (target[start] != target[end]) {
alreadyPalindrome = false;
target[end] = target[start];
}
start++;
end--;
}
if (alreadyPalindrome) {
if (ifNeedToIncrementPlaces(target)) {
return getPalindromeOnSizeIncrement(target.length);
} else {
incrementForNextPalindrome(target);
return target;
}
}
if (greater(sourceNumber, target)) {
incrementForNextPalindrome(target);
}
return target;
}
public static void incrementForNextPalindrome(char[] target) {
System.out.println(target);
int length = target.length;
int placeToIncrement = length / 2;
boolean singleValueToChange = length % 2 == 0 ? false : true;
boolean odd = length % 2 == 0 ? false : true;
// this function will not index overrun because it will only be called
// in the case where all digits are not 9
int shiftPlacesFor9 = 0;
while (target[placeToIncrement] == NINE) {
singleValueToChange = false;
if (odd) {
target[placeToIncrement - shiftPlacesFor9] = ZERO;
target[placeToIncrement + shiftPlacesFor9] = ZERO;
} else {
target[placeToIncrement - shiftPlacesFor9 - 1] = ZERO;
target[placeToIncrement + shiftPlacesFor9] = ZERO;
}
shiftPlacesFor9++;
}
placeToIncrement = placeToIncrement + shiftPlacesFor9;
if (!singleValueToChange) { // even length
target[length - 1 - placeToIncrement] = (char) ((int) (target[length
- 1 - placeToIncrement]) + 1);
target[placeToIncrement] = target[length - 1 - placeToIncrement];
} else {
target[placeToIncrement] = (char) ((int) (target[placeToIncrement]) + 1);
}
}
public static boolean greater(char[] first, char[] second) {
// values passed here will alwayas be of same sizes, so not null and
// size check
for (int fItr = 0, sItr = 0; fItr < first.length; fItr++, sItr++) {
if (first[fItr] > second[sItr]) {
return true;
}
}
return false;
}
public static boolean ifNeedToIncrementPlaces(char[] target) {
boolean needToIncrement = true;
for (int i = 0; i < target.length; i++) {
if (target[i] != '9') {
needToIncrement = false;
break;
}
}
return needToIncrement;
}
public static char[] getPalindromeOnSizeIncrement(int length) {
char[] result = new char[length + 1];
Arrays.fill(result, '0');
result[0] = '1';
result[result.length - 1] = '1';
return result;
}
public static void main(String[] args) {
// char[] source = new char[]{};
char[] source = new char[] { '1', '9', '1' };
System.out.println(PalindromeUtil.nextPalindrome(source));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment