Check if all elements of the array are palindrome or not Last Updated : 30 Dec, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Try it on GfG Practice 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: falseExplanation: 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. By Checking Each Element - O(n*k) Time and O(1) SpaceThe 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'); OutputtrueComplexity Analysis:Time Complexity: O(n*k) n is for traversing the array and k is for checking each element.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Check if all elements of the array are palindrome or not R rachana soma Follow Improve Article Tags : Technical Scripter DSA Arrays palindrome Practice Tags : Arrayspalindrome Similar Reads Check if both halves of a string are Palindrome or not Given a string str, the task is to check whether the given string can be split into two halves, each of which is palindromic. If it is possible, print Yes. Otherwise, print No. Examples: Input: str = "naan" Output: No Explanation: Since both halves "na" and "an" are not palindrome. Input: momdad Out 5 min read Check if rearranging Array elements can form a Palindrome or not Given a positive integer array arr of size N, the task is to check if number formed, from any arrangement of array elements, form a palindrome or not. Examples: Input: arr = [1, 2, 3, 1, 2]Output: YesExplanation: The elements of a given array can be rearranged as 1, 2, 3, 2, 1. Since 12321 is a pali 7 min read Check if all the diagonals of the Matrix are palindromic or not Given a matrix mat[][] of dimensions N*M, the task is to check if all the diagonals of the matrix(from top-right to bottom-left) are palindromic or not. If found to be true, then print Yes. Otherwise, print No. Examples: Input: mat[][] = [[1, 0, 0, 0], [0, 1, 1, 1], [0, 1, 0, 1], [0, 1, 1, 0]]Output 11 min read Check if any anagram of a string is palindrome or not Given an anagram string S of length N, the task is to check whether it can be made palindrome or not. Examples: Input : S = geeksforgeeks Output: NoExplanation: There is no palindrome anagram of given string Input: S = geeksgeeksOutput: YesExplanation: There are palindrome anagrams of given string. 8 min read Program to check if an Array is Palindrome or not Given an array, the task is to determine whether an array is a palindrome or not.Examples: Input: arr[] = {3, 6, 0, 6, 3} Output: Palindrome Input: arr[] = {1, 2, 3, 4, 5} Output: Not Palindrome Approach: Initialise flag to unset int flag = 0.Loop the array till size n/2In a loop check if arr[i]! = 5 min read Like