Array elements form a palindrome

Last Updated : 19 Apr, 2026

Given an array, the task is to determine whether an array is a palindrome or not.
Examples: 

Input: arr[] = [1, 2, 3, 2, 1]
Output: true
Explanation: Elements match from both ends, 3==3, 6==6, middle 0, all match.

Input: arr[] = [1, 2, 3, 4, 5]
Output: false
Explanation: First and last elements differ, 1 != 5, so it is not a palindrome.

Try It Yourself
redirect icon

Two Pointer Technique - O(n) Time and O(1) Space

Traverse the array from index 0 to n/2, comparing each element arr[i] with its corresponding element from the end arr[n - i - 1]. If any pair does not match, the array is not a palindrome; otherwise, it is a palindrome.

C++
#include <iostream>
#include <vector>
using namespace std;

bool isPalindrome(vector<int> &arr) {
    int n = arr.size();
    
    // Traverse till middle
    for (int i = 0; i <= n / 2; i++) {
        
        // Check for mismatch
        if (arr[i]!= arr[n - i - 1]) {
            return false;
        }
    }
    
    // If all elements match
    return true;
}

// Driver code
int main() {
    vector<int> arr = {1, 2, 3, 2, 1};
    
  
        cout << boolalpha << isPalindrome(arr);
    
    return 0;
}
Java
import java.util.*;

class GfG {

    public static boolean
    isPalindrome(ArrayList<Integer> arr)
    {
        int n = arr.size();

        // Traverse till middle
        for (int i = 0; i <= n / 2; i++) {

            // Check for mismatch
            if (!arr.get(i).equals(arr.get(n - i - 1))) {
                return false;
            }
        }

        // If all elements match
        return true;
    }

    // Driver code
    public static void main(String[] args)
    {
        ArrayList<Integer> arr
            = new ArrayList<>(Arrays.asList(1, 2, 3, 2, 1));

        System.out.println(isPalindrome(arr));
    }
}
Python
def isPalindrome(arr):
    n = len(arr)

    # Traverse till middle
    for i in range(n // 2):

        # Check for mismatch
        if arr[i] != arr[n - i - 1]:
            return False

    # If all elements match
    return True


# Driver code
if __name__ == "__main__":
    arr = [1, 2, 3, 2, 1]

    print(isPalindrome(arr))
C#
using System;
using System.Collections.Generic;

class GfG {
    
    public static bool isPalindrome(List<int> arr) {
        int n = arr.Count;
        
        // Traverse till middle
        for (int i = 0; i <= n / 2; i++) {
            
            // Check for mismatch
            if (arr[i] != arr[n - i - 1]) {
                return false;
            }
        }
        
        // If all elements match
        return true;
    }

    public static void Main() {
        List<int> arr = new List<int> {1, 2, 3, 2, 1};
        
        
            Console.WriteLine(isPalindrome(arr));
    
    }
}
JavaScript
function isPalindrome(arr)
{
    let n = arr.length;

    // Traverse till middle
    for (let i = 0; i <= Math.floor(n / 2); i++) {

        // Check for mismatch
        if (arr[i] !== arr[n - i - 1]) {
            return false;
        }
    }

    // If all elements match
    return true;
}

// Driver code
let arr = [ 1, 2, 3, 2, 1 ];

console.log(isPalindrome(arr));
PHP
<?php

function isPalindrome($arr) {
    $n = count($arr);
    
    // Traverse till middle
    for ($i = 0; $i <= intdiv($n, 2); $i++) {
        
        // Check for mismatch
        if ($arr[$i]!= $arr[$n - $i - 1]) {
            return false;
        }
    }
    
    // If all elements match
    return true;
}

$arr = [1, 2, 3, 2, 1];

if (isPalindrome($arr)) {
    echo "true";
} else {
    echo "false";
}
?>

Output
Palindrome

Using Recursion - O(n) Time and O(n) Space 

First compares the first and last elements, and if they match, then recursively check the remaining subarray by moving inward by begin + 1, end - 1. If all pairs match, the array is a palindrome.

C++
#include <iostream>
#include <vector>
using namespace std;

bool checkPalindrome(vector<int> &arr, int begin, int end)
{

    // Base case
    if (begin >= end)
        return true;

    // If mismatch found
    if (arr[begin] != arr[end])
        return false;

    // Recursive call
    return checkPalindrome(arr, begin + 1, end - 1);
}

bool isPalindrome(vector<int> &arr)
{
    return checkPalindrome(arr, 0, arr.size() - 1);
}


// Driver code
int main()
{
    vector<int> arr = {1, 2, 3, 2, 1};

    cout << boolalpha << isPalindrome(arr);

    return 0;
}
Java
import java.util.*;

class GfG {

    static boolean checkPalindrome(ArrayList<Integer> arr,
                                   int begin, int end)
    {

        // Base case
        if (begin >= end)
            return true;

        // If mismatch found
        if (!arr.get(begin).equals(arr.get(end)))
            return false;

        // Recursive call
        return checkPalindrome(arr, begin + 1, end - 1);
    }

    static boolean isPalindrome(ArrayList<Integer> arr)
    {
        return checkPalindrome(arr, 0, arr.size() - 1);
    }

    public static void main(String[] args)
    {
        ArrayList<Integer> arr
            = new ArrayList<>(Arrays.asList(1, 2, 3, 2, 1));

        System.out.println(isPalindrome(arr));
    }
}
Python
def checkPalindrome(arr, begin, end):
    # Base case
    if begin >= end:
        return True

    # If mismatch found
    if arr[begin] != arr[end]:
        return False

    # Recursive call
    return checkPalindrome(arr, begin + 1, end - 1)


def isPalindrome(arr):
    return checkPalindrome(arr, 0, len(arr) - 1)


if __name__ == '__main__':
    arr = [1, 2, 3, 2, 1]

    print(isPalindrome(arr))
C#
using System;
using System.Collections.Generic;

class GfG {

    static bool CheckPalindrome(List<int> arr, int begin,
                                int end)
    {

        // Base case
        if (begin >= end)
            return true;

        // If mismatch found
        if (arr[begin] != arr[end])
            return false;

        // Recursive call
        return CheckPalindrome(arr, begin + 1, end - 1);
    }

    static bool isPalindrome(List<int> arr)
    {
        return CheckPalindrome(arr, 0, arr.Count - 1);
    }

    public static void Main()
    {
        List<int> arr = new List<int>{ 1, 2, 3, 2, 1 };

        Console.WriteLine(isPalindrome(arr));
    }
}
JavaScript
function checkPalindrome(arr, begin, end)
{
    // Base case
    if (begin >= end)
        return true;

    // If mismatch found
    if (arr[begin] !== arr[end])
        return false;

    // Recursive call
    return checkPalindrome(arr, begin + 1, end - 1);
}

function isPalindrome(arr)
{
    return checkPalindrome(arr, 0, arr.length - 1);
}

// Driver code
const arr = [ 1, 2, 3, 2, 1 ];

console.log(isPalindrome(arr));

Output
Palindrome
Comment