C Program to check whether an array is a palindrome
Learn how to check whether a C array is a palindrome by comparing mirrored elements with two pointers.
An array is a palindrome when its elements read the same from left to right and from right to left.
For example:
Palindrome: 4 7 9 7 4
Not palindrome: 2 5 8 5 3
In the first array, the first and last elements match, the second and second-last elements match, and the middle element does not need a partner. In the second array, the outer values 2 and 3 are different, so it is not a palindrome.
C Program to Check Whether an Array Is a Palindrome
#include <stdio.h>
#define MAX_SIZE 100
int main(void) {
int array[MAX_SIZE];
int size;
int isPalindrome = 1;
printf("Enter the number of elements: ");
if (scanf("%d", &size) != 1 || size < 1 || size > MAX_SIZE) {
printf("Please enter a size between 1 and %d.\n", MAX_SIZE);
return 1;
}
printf("Enter %d elements:\n", size);
for (int index = 0; index < size; index++) {
if (scanf("%d", &array[index]) != 1) {
printf("Invalid array element.\n");
return 1;
}
}
int left = 0;
int right = size - 1;
while (left < right) {
if (array[left] != array[right]) {
isPalindrome = 0;
break;
}
left++;
right--;
}
if (isPalindrome) {
printf("The array is a palindrome.\n");
} else {
printf("The array is not a palindrome.\n");
}
return 0;
}
Sample Output for a Palindrome Array
Enter the number of elements: 5
Enter 5 elements:
4 7 9 7 4
The array is a palindrome.
Sample Output for a Non-Palindrome Array
Enter the number of elements: 5
Enter 5 elements:
2 5 8 5 3
The array is not a palindrome.
How the Program Works
The program uses two indices that begin at opposite ends of the array:
leftstarts at index0.rightstarts at indexsize - 1.
The elements at those positions are compared. If they match, both indices move one step toward the center. If they do not match, the array cannot be a palindrome, so the flag is cleared and the loop stops.
while (left < right) {
if (array[left] != array[right]) {
isPalindrome = 0;
break;
}
left++;
right--;
}
If every mirrored pair matches, isPalindrome remains 1.
Dry Run for an Odd-Sized Array
Consider 4 7 9 7 4:
left | right | Left value | Right value | Result |
|---|---|---|---|---|
| 0 | 4 | 4 | 4 | Match; move inward |
| 1 | 3 | 7 | 7 | Match; move inward |
| 2 | 2 | 9 | 9 | Pointers meet; stop |
Only two comparisons are required. The middle value 9 automatically matches itself, so comparing it is unnecessary.
Dry Run with an Early Mismatch
Now consider 2 5 8 5 3:
left | right | Left value | Right value | Result |
|---|---|---|---|---|
| 0 | 4 | 2 | 3 | Mismatch; not a palindrome |
The program stops after the first comparison. Later matching values cannot repair a mismatched outer pair.
Why Are Only Half the Elements Compared?
Every comparison checks two positions at once: one from the left half and one from the right half. After the pointers meet or cross, every required mirrored pair has already been checked.
For an array of n elements, the program makes at most n / 2 comparisons. Checking the same pairs again from the opposite direction would add work without changing the answer.
How Are Mirrored Positions Related?
The mirror of index i in an array of size n is:
n - 1 - i
For an array of size 5:
| Index | Mirrored index |
|---|---|
| 0 | 4 |
| 1 | 3 |
| 2 | 2 |
The right pointer begins at size - 1 and moves left, so the program performs these comparisons without repeatedly calculating the formula.
Odd and Even Array Sizes
The same loop works for both sizes.
An odd-sized palindrome has one unpaired center element:
1 6 3 6 1
An even-sized palindrome has only mirrored pairs:
1 6 6 1
For an odd size, the pointers eventually meet. For an even size, they cross after checking the innermost pair. The condition left < right handles both cases correctly.
What Happens with One Element?
Every one-element array is a palindrome because it reads the same in both directions:
Array: 42
Result: palindrome
Here, left and right are both 0, so the loop does not execute and isPalindrome remains 1.
Do Duplicate Values Make an Array a Palindrome?
Not necessarily. A palindrome requires values to match at mirrored positions, not merely to appear more than once.
1 2 1 2
This array contains duplicates, but it is not a palindrome because the first value 1 does not match the last value 2.
Does It Work with Negative Numbers and Zero?
Yes. The program compares integer values directly, so negative numbers and zero require no special handling:
-3 0 8 0 -3
This array is a palindrome because every mirrored pair is equal.
The Original Array Is Not Modified
The program only reads elements during the palindrome check. It does not reverse the array, swap values, or create a second copy. The input remains in its original order after the result is printed.
This is useful when later parts of a program still need the original array.
Alternative Method Using a Reversed Copy
Another method is to create a second array in reverse order and compare it with the original:
int reversed[MAX_SIZE];
for (int index = 0; index < size; index++) {
reversed[index] = array[size - 1 - index];
}
This approach is valid, but it requires O(n) additional space and writes every element before comparison. The two-pointer method obtains the same answer with O(1) extra space and can stop immediately on a mismatch.
Palindrome Check Versus Reversing an Array
These operations are related but different:
- Palindrome check: Determines whether the forward and reverse orders are equal without changing the array.
- Array reversal: Rearranges the elements so that their order becomes the opposite of the original.
Reversing the array is unnecessary when only a yes-or-no palindrome result is needed.
Time and Space Complexity
For an array containing n elements:
- Best-case time complexity:
O(1)when the first and last elements differ. - Worst-case time complexity:
O(n)when every mirrored pair must be checked. - Extra space complexity:
O(1)because only two indices and one flag are used.
Common Mistakes
- Comparing only neighboring values instead of mirrored values.
- Using
size - indexas the mirrored index instead ofsize - 1 - index. - Checking every element and repeating each comparison twice.
- Forgetting to initialize
isPalindrometo1. - Continuing after a mismatch even though the answer is already known.
- Reversing or modifying the original array when a read-only comparison is sufficient.
- Assuming that an array with duplicate values must be a palindrome.
By comparing values from both ends and moving toward the center, the program checks whether an array is a palindrome efficiently without modifying it or allocating another array.