beginnerArray ProblemsJava

Check Whether an Array Is a Palindrome

Determine whether an array reads the same forwards and backwards using two pointers.

Quick Answer

Use two indices, one at the start and one at the end. Compare the elements they point to; if they ever differ, the array is not a palindrome. Move the pointers toward each other and stop when they meet. If all compared pairs matched, it is a palindrome.

Problem Statement

Given an array, determine whether it is a palindrome — whether it reads the same from left to right as from right to left.

Return true if it is a palindrome and false otherwise. Use two pointers from opposite ends so the check needs only O(1) extra space and stops early on a mismatch.

Input: An array arr of length n.

Output: A boolean: true if the array is a palindrome, false otherwise.

Examples

Example 1
Input:  [1, 2, 3, 2, 1]
Output: true

Reading from either end gives 1, 2, 3, 2, 1 — the pairs (1,1) and (2,2) match and the middle 3 is unpaired.

Example 2
Input:  [1, 2, 3, 4]
Output: false

The first pair 1 and 4 already differ, so it is not a palindrome.

Constraints

  • 0 <= n <= 10^6
  • Extra space must be O(1)

Think Before You Code

Reveal the questions to ask yourself first
  • Which pairs of elements must be equal for a palindrome?
  • When can you stop comparing?
  • Can you answer as soon as you find a single mismatch?

Hints

Open them one at a time — try after each before revealing the next.

Hint 1
Mirror positions must match: the first with the last, the second with the second-last, and so on.
Hint 2
Use `left = 0` and `right = n - 1`, and loop while `left < right`.
Hint 3
If `arr[left] != arr[right]`, return false; otherwise move `left++` and `right--`.

Approach

Reveal the step-by-step approach

Compare mirror-image pairs from the outside in.

  1. Set left = 0 and right = n - 1.
  2. While left < right:
    • If arr[left] != arr[right], return false.
    • Otherwise left++ and right--.
  3. If the loop finishes with no mismatch, return true.

The middle element of an odd-length array is never compared with anything, which is correct because it mirrors itself.

Dry Run

Walk through the example step by step

Checking [1, 2, 3, 2, 1]:

left | right | arr[left] | arr[right] | match?
-----+-------+-----------+------------+-------
0    | 4     | 1         | 1          | yes
1    | 3     | 2         | 2          | yes
2    | 2     | left==right, stop
------------------------------------- result true

Solution

Reveal the full Java solution
public class ArrayPalindrome {
    public static boolean isPalindrome(int[] arr) {
        int left = 0;
        int right = arr.length - 1;
        while (left < right) {
            if (arr[left] != arr[right]) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(isPalindrome(new int[]{1, 2, 3, 2, 1})); // true
        System.out.println(isPalindrome(new int[]{1, 2, 3, 4}));    // false
    }
}

A palindrome is defined by its mirror-image pairs being equal, so comparing from both ends toward the centre is both sufficient and efficient. Returning false on the first mismatch avoids scanning the rest, and using indices instead of a reversed copy keeps the extra space at O(1).

Time: O(n)Space: O(1)

Common Mistakes

  • Building a reversed copy and comparing arrays, which wastes O(n) extra space.
  • Looping while `left <= right`, comparing the middle element with itself needlessly (harmless but wasteful, and error-prone if bounds shift).
  • Forgetting to move both pointers, causing an infinite loop.

Edge Cases to Test

  • Empty array [] and single element [9] are palindromes (true).
  • Even-length palindromes like [1, 2, 2, 1].
  • Odd-length arrays where the unpaired middle element does not affect the result.

Interview Follow-Ups

  • How would you check whether an array is a palindrome using recursion?
  • How would you check a String palindrome with the same two-pointer idea?
  • How would you find the longest palindromic sub-array?

Practising for Java interviews?

CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.

Explore the Java Full Stack program →
Chat with us