A palindrome is a word, number or sequence which has same order in backward or forward direction. Given an input integer array arr, check if the array elements are arranged like a palindrome. Required time complexity is O(n).
| Input Parameters |
Expected outputs |
| arr={1,2,3,4,3,2,1}
|
The array is a Palindrome.
|
| arr={9,-1,-1,9}
|
The array is a Palindrome.
|
| arr={1,3,5,7,9,11,17 }
|
The array is not a Palindrome.
|
| arr={1,3,5,7,5,3,0 }
|
The array is not a Palindrome.
|
| The java method should accept following input parameters: arr (int array).
|
| Initialize int variable leftIdx to 0 and int variable rightIdx to arr.length-1.
|
| Initialize a boolean variable isPalindome to false.
|
| Compare the value of array element at leftIdx with value at rightIdx. If they are the same, then the check for palindrome elements has passed for current indexes, so set isPalindome to true. If they are not same, check for palindrome has failed, set isPalindome to false. If check for palindrome fails even once, no further checking is required.
|
| Increment leftIdx by 1 and decrement rightIdx by 1. Again, repeat the check in step above.
|
| Repeat above steps using while loop till leftIdx < rightIdx.
|
| After while loop ends return the value of isPalindome.
|
Below fully running code can be copied and run on Eclipse or other Java IDEs. Refer the classname in code below. If the class name below is "A", save the code below to a file named A.java before running it.
Be sure to try your own test cases to enhance your understanding !
You can also tweak the code to optimize or add enhancements and custom features.
public class ArrayCheckPalindrome {
public boolean arrayCheckPalindrome(int[] arr) throws Exception{
int leftIdx=0;
int rightIdx=arr.length-1;
boolean isPalindrome=true;
while (leftIdx < rightIdx) {
if (arr[leftIdx]==arr[rightIdx]) {
leftIdx++;
rightIdx--;
continue;
}else {
isPalindrome=false;
break;
}
}
if (isPalindrome==true) {
return true;
}else {
return false;
}
}
public static void main(String[] args) {
ArrayCheckPalindrome ap= new ArrayCheckPalindrome();
boolean arrayIsPalindrome=false;
try {
int[] intArray1 = {1,2,3,4,3,2,1};
printArraySummary(intArray1, "Original Array");
arrayIsPalindrome=ap.arrayCheckPalindrome(intArray1);
if (arrayIsPalindrome==true) {
System.out.println("\n The array is a Palindrome !\n");
}else {
System.out.println("\n The array is *not* a Palindrome !\n");
}
int[] intArray2 = {9,-1,-1,9};
printArraySummary(intArray2, "Original Array");
arrayIsPalindrome=ap.arrayCheckPalindrome(intArray2);
if (arrayIsPalindrome==true) {
System.out.println("\n The array is a Palindrome !\n");
}else {
System.out.println("\n The array is *not* a Palindrome !\n");
}
int[] intArray3 = {4,5,4};
printArraySummary(intArray3, "Original Array");
arrayIsPalindrome=ap.arrayCheckPalindrome(intArray3);
if (arrayIsPalindrome==true) {
System.out.println("\n The array is a Palindrome !\n");
}else {
System.out.println("\n The array is *not* a Palindrome !\n");
};
int[] intArray4 = {0,0,0,0,0};
printArraySummary(intArray4, "Original Array");
arrayIsPalindrome=ap.arrayCheckPalindrome(intArray4);
if (arrayIsPalindrome==true) {
System.out.println("\n The array is a Palindrome !\n");
}else {
System.out.println("\n The array is *not* a Palindrome !\n");
}
int[] intArray5 = {1};
printArraySummary(intArray5, "Original Array");
arrayIsPalindrome=ap.arrayCheckPalindrome(intArray5);
if (arrayIsPalindrome==true) {
System.out.println("\n The array is a Palindrome !\n");
}else {
System.out.println("\n The array is *not* a Palindrome !\n");
}
int[] intArray6 = {};
printArraySummary(intArray6, "Original Array");
arrayIsPalindrome=ap.arrayCheckPalindrome(intArray6);
if (arrayIsPalindrome==true) {
System.out.println("\n The array is a Palindrome !\n");
}else {
System.out.println("\n The array is *not* a Palindrome !\n");
}
int[] intArray7 = {1,3,5,7,9,11,17};
printArraySummary(intArray7, "Original Array");
arrayIsPalindrome=ap.arrayCheckPalindrome(intArray7);
if (arrayIsPalindrome==true) {
System.out.println("\n The array is a Palindrome !\n");
}else {
System.out.println("\n The array is *not* a Palindrome !\n");
}
int[] intArray8 = {1,3,5,7,5,3,0};
printArraySummary(intArray8, "Original Array");
arrayIsPalindrome=ap.arrayCheckPalindrome(intArray8);
if (arrayIsPalindrome==true) {
System.out.println("\n The array is a Palindrome !\n");
}else {
System.out.println("\n The array is *not* a Palindrome !\n");
}
}catch (Exception exception) {
System.out.print("Exception: "+ exception);
}
}
public static void printArraySummary(int[] intArray, String label) throws Exception {
// Case 1: The input Array is null !!
if (intArray == null) { System.out.println("\n\n Input Array was null !! \n"); return; }
// Case 2: Print input Array by index (first to last)
System.out.println();
System.out.println("************************************************************************");
System.out.print(label+" : ");
int arrayIndex=0;
for (arrayIndex=0; arrayIndex< intArray.length; arrayIndex++) {
System.out.print(intArray[arrayIndex]);
if (arrayIndex< intArray.length-1) {System.out.print(",");}
}
System.out.println();
System.out.println("*************************************************************************");
System.out.println();
Thread.sleep(2000);
return;
}
}