Check if all elements of the array are palindrome or not

Last Updated : 25 Oct, 2026

Given an array arr[] of positive integers. Return true if all the array elements are palindrome otherwise, return false.

Examples: 

Input: arr[] = [111, 222, 333, 444, 555]
Output: true
Explanation:
arr[0] = 111, which is a palindrome number.
arr[1] = 222, which is a palindrome number.
arr[2] = 333, which is a palindrome number.
arr[3] = 444, which is a palindrome number.
arr[4] = 555, which is a palindrome number.
As all numbers are palindrome so This will return true.

Input: arr[] = [121, 131, 20]
Output: false
Explanation: 20 is not a palindrome hence the output is false.

Try It Yourself
redirect icon

[Naive Approach] Check Each String - O(n * d) Time and O(d) Space

The idea is to convert each number into a string and compare characters from ends.

Working of Approach:

  • Traverse each element of the array.
  • Convert the element into a string.
  • Check each string by comparing characters from both ends.
  • If all elements are palindromes, return true.
C++
#include <bits/stdc++.h>
using namespace std;

bool isPalindrome(int n)
{
    string s = to_string(n);
    int len = s.length();

    for (int i = 0; i < len / 2; i++)
    {
        if (s[i] != s[len - 1 - i])
            return false;
    }
    return true;
}

bool isPalinArray(vector<int> &arr)
{
    // Traversing each element of the array
    // and checking if it is palindrome or not
    for (int i = 0; i < arr.size(); i++)
    {
        if (!isPalindrome(arr[i]))
            return false;
    }
    return true;
}

int main()
{
    vector<int> arr = {111, 222, 333, 444, 555};

    cout << (isPalinArray(arr) ? "true" : "false");

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

public class GFG {
    // Function to check if a number is palindrome
    public static boolean isPalindrome(int n)
    {
        String s = Integer.toString(n);
        int len = s.length();

        for (int i = 0; i < len / 2; i++) {
            if (s.charAt(i) != s.charAt(len - 1 - i))
                return false;
        }
        return true;
    }

    // Function to check if all elements of the array are
    // palindrome
    public static boolean isPalinArray(int[] arr)
    {
        // Traversing each element of the array
        // and checking if it is palindrome or not
        for (int i = 0; i < arr.length; i++) {
            if (!isPalindrome(arr[i]))
                return false;
        }
        return true;
    }

    public static void main(String[] args)
    {
        int[] arr = { 111, 222, 333, 444, 555 };

        System.out.println(isPalinArray(arr) ? "true"
                                             : "false");
    }
}
Python
def isPalindrome(n):
    # Function to check if a number is palindrome
    s = str(n)
    len_s = len(s)

    for i in range(len_s // 2):
        if s[i]!= s[len_s - 1 - i]:
            return False
    return True

def isPalinArray(arr):
    # Function to check if all elements of the array are palindrome
    # Traversing each element of the array
    # and checking if it is palindrome or not
    for i in range(len(arr)):
        if not isPalindrome(arr[i]):
            return False
    return True

if __name__ == '__main__':
    arr = [111, 222, 333, 444, 555]

    print('true' if isPalinArray(arr) else 'false')
C#
using System;

public class GFG {
    // Function to check if a number is palindrome
    public static bool IsPalindrome(int n)
    {
        string s = n.ToString();
        int len = s.Length;

        for (int i = 0; i < len / 2; i++) {
            if (s[i] != s[len - 1 - i])
                return false;
        }
        return true;
    }

    // Function to check if all elements of the array are
    // palindrome
    public static bool isPalinArray(int[] arr)
    {
        // Traversing each element of the array
        // and checking if it is palindrome or not
        for (int i = 0; i < arr.Length; i++) {
            if (!IsPalindrome(arr[i]))
                return false;
        }
        return true;
    }

    public static void Main()
    {
        int[] arr = { 111, 222, 333, 444, 555 };

        Console.WriteLine(isPalinArray(arr) ? "true"
                                            : "false");
    }
}
JavaScript
function isPalindrome(n)
{
    // Function to check if a number is palindrome
    let s = n.toString();
    let len = s.length;

    for (let i = 0; i < Math.floor(len / 2); i++) {
        if (s[i] !== s[len - 1 - i])
            return false;
    }
    return true;
}

function isPalinArray(arr)
{
    // Function to check if all elements of the array are
    // palindrome Traversing each element of the array and
    // checking if it is palindrome or not
    for (let i = 0; i < arr.length; i++) {
        if (!isPalindrome(arr[i]))
            return false;
    }
    return true;
}

// Driver Code
const arr = [ 111, 222, 333, 444, 555 ];
console.log(isPalinArray(arr) ? "true" : "false");

Output
true

[Expected Approach] Reverse Each Mathematically - O(n * d) Time and O(1) Space

The idea is to reverse each number using arithmetic operations and compare it with the original number.

Working of Approach:

  • Traverse each element of the array.
  • Store the original number and initialize the reversed number to 0.
  • Extract the last digit using % 10 and append it to the reversed number.
  • If the reversed number differs from the original, return false.
  • If all elements are palindromes, return true.
C++
#include <bits/stdc++.h>
using namespace std;

// Function to check if a number is a palindrome
bool check(int a)
{
    int m = a;
    int rev = 0;

    // Reverse the number
    while (a != 0)
    {
        rev = rev * 10 + (a % 10);
        a /= 10;
    }

    // Compare the reversed number with the original
    return m == rev;
}

// Function to check if all array elements are palindromes
bool isPalinArray(vector<int> &arr)
{
    // Check each array element
    for (int i = 0; i < arr.size(); i++)
    {
        if (!check(arr[i]))
            return false;
    }

    // All elements are palindromes
    return true;
}

int main()
{
    vector<int> arr = {111, 222, 333, 444, 555};

    cout << (isPalinArray(arr) ? "true" : "false");

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

// Function to check if a number is a palindrome
public class GFG {
    public static boolean check(int a)
    {
        int m = a;
        int rev = 0;

        // Reverse the number
        while (a != 0) {
            rev = rev * 10 + (a % 10);
            a /= 10;
        }

        // Compare the reversed number with the original
        return m == rev;
    }

    // Function to check if all array elements are
    // palindromes
    public static boolean isPalinArray(int[] arr)
    {
        // Check each array element
        for (int i = 0; i < arr.length; i++) {
            if (!check(arr[i]))
                return false;
        }

        // All elements are palindromes
        return true;
    }

    public static void main(String[] args)
    {
        int[] arr = { 111, 222, 333, 444, 555 };

        System.out.println(isPalinArray(arr) ? "true"
                                             : "false");
    }
}
Python
# Function to check if a number is a palindrome
def check(a):
    m = a
    rev = 0

    # Reverse the number
    while a!= 0:
        rev = rev * 10 + (a % 10)
        a //= 10

    # Compare the reversed number with the original
    return m == rev

# Function to check if all array elements are palindromes
def isPalinArray(arr):
    # Check each array element
    for i in range(len(arr)):
        if not check(arr[i]):
            return False

    # All elements are palindromes
    return True

if __name__ == "__main__":
    arr = [111, 222, 333, 444, 555]

    print("true" if isPalinArray(arr) else "false")
C#
using System;

// Function to check if a number is a palindrome
public class GFG {
    public static bool Check(int a)
    {
        int m = a;
        int rev = 0;

        // Reverse the number
        while (a != 0) {
            rev = rev * 10 + (a % 10);
            a /= 10;
        }

        // Compare the reversed number with the original
        return m == rev;
    }

    // Function to check if all array elements are
    // palindromes
    public static bool isPalinArray(int[] arr)
    {
        // Check each array element
        for (int i = 0; i < arr.Length; i++) {
            if (!Check(arr[i]))
                return false;
        }

        // All elements are palindromes
        return true;
    }

    public static void Main()
    {
        int[] arr = { 111, 222, 333, 444, 555 };

        Console.WriteLine(isPalinArray(arr) ? "true"
                                            : "false");
    }
}
JavaScript
// Function to check if a number is a palindrome
function check(a)
{
    let m = a;
    let rev = 0;

    // Reverse the number
    while (a != 0) {
        rev = rev * 10 + (a % 10);
        a = Math.floor(a / 10);
    }

    // Compare the reversed number with the original
    return m === rev;
}

// Function to check if all array elements are palindromes
function isPalinArray(arr)
{
    // Check each array element
    for (let i = 0; i < arr.length; i++) {
        if (!check(arr[i]))
            return false;
    }

    // All elements are palindromes
    return true;
}

// Driver Code
const arr = [ 111, 222, 333, 444, 555 ];
console.log(isPalinArray(arr) ? "true" : "false");

Output
true
Comment