Check if all elements of the array are palindrome or not (original) (raw)

Last Updated : 30 Dec, 2024

Given an array **arr[]. The task is to check if the array is **PalinArray or not i.e., if all elements of array are palindrome or not.

**Examples:

**Input: arr[] = [21, 131, 20]
**Output: false
**Explanation: For the given array, element 20 is not a palindrome so false is the output.

**Input: arr[] = [111, 121, 222, 333, 444]
**Output: true
**Explanation: For the given array, all the elements of the array are palindromes.

Try It Yourselfredirect icon

By Checking Each Element - O(n*k) Time and O(1) Space

The basic idea is to check each number in the array is a palindrome by converting each number to a string and comparing its characters symmetrically from both ends. If any non-palindrome is found then returns false otherwise true.

C++ `

#include<bits/stdc++.h> using namespace std;

bool isPalindrome(int n) {
    string s = "" + 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 check 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 = { 121, 131, 20 };
    bool res = isPalinArray(arr);
    if (res == true)
        cout<<"true";
    else
        cout<<"false";
}

Java

class GfG { 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; }

public static boolean isPalinArray(int[] arr) {
  
    // Traversing each element of the array
    // and check 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 = { 121, 131, 20 };
    boolean res = isPalinArray(arr);
    System.out.println(res);
}

}

Python

def isPalindrome(n): s = str(n) return s == s[::-1]

def isPalinArray(arr):

# Traversing each element of the array
# and check if it is palindrome or not
for num in arr:
    if not isPalindrome(num):
        return False
return True

if name == "main": arr = [121, 131, 20] res = isPalinArray(arr) print("true" if res else "false")

C#

using System; using System.Linq;

class GfG { static bool IsPalindrome(int n) { string s = n.ToString(); return s.SequenceEqual(s.Reverse()); }

static bool IsPalinArray(int[] arr) {
  
    // Traversing each element of the array
    // and check if it is palindrome or not
    foreach (var num in arr) {
        if (! IsPalindrome(num))
            return false;
    }
    return true;
}

static void Main() {
    int[] arr = { 121, 131, 20 };
    bool res = IsPalinArray(arr);
    Console.WriteLine(res ? "true" : "false");
}

}

JavaScript

function isPalindrome(n) { let s = n.toString(); return s === s.split('').reverse().join(''); }

function isPalinArray(arr) {

// Traversing each element of the array
// and check if it is palindrome or not
for (let num of arr) {
    if (! isPalindrome(num))
        return false;
}
return true;

}

// Driver Code const arr = [121, 131, 20]; const res = isPalinArray(arr); console.log(res ? 'true' : 'false');

`

**Complexity Analysis: