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 Yourself
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.
#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";
}
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);
}
}
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")
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");
}
}
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');
Output
true
Complexity Analysis:
- Time Complexity: O(n*k) n is for traversing the array and k is for checking each element.
- Auxiliary Space: O(1)