Check if all elements of the array are palindrome or not
Last Updated :
13 Sep, 2022
Given an array arr[] of N elements. 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[] = {121, 131, 20}
Output: Array is not a PalinArray
For the given array, element 20 is not a palindrome. Thus the array is not a PalinArray.
Input: arr[] = {111, 121, 222, 333, 444}
Output: Array is a PalinArray
For the given array, all the elements of the array are palindromes. Thus the array is a PalinArray.
Approach:
- Traverse all elements of the given array and check if each is a palindrome or not.
- If yes, print Array is a PalinArray.
- Else, print Array is not a PalinArray.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
bool isPalindrome( int N)
{
string str = "" + N;
int len = str.length();
for ( int i = 0; i < len / 2; i++) {
if (str[i] != str[len - 1 - i])
return false ;
}
return true ;
}
bool isPalinArray( int arr[] , int n)
{
for ( int i = 0; i < n; i++) {
bool ans = isPalindrome(arr[i]);
if (ans == false )
return false ;
}
return true ;
}
int main()
{
int arr[] = { 121, 131, 20 };
int n = sizeof (arr)/ sizeof (arr[0]);
bool res = isPalinArray(arr, n);
if (res == true )
cout<< "Array is a PalinArray" ;
else
cout<< "Array is not a PalinArray" ;
}
|
Java
class GFG {
static boolean isPalindrome( int N)
{
String str = "" + N;
int len = str.length();
for ( int i = 0 ; i < len / 2 ; i++) {
if (str.charAt(i) != str.charAt(len - 1 - i))
return false ;
}
return true ;
}
static boolean isPalinArray( int [] arr, int n)
{
for ( int i = 0 ; i < n; i++) {
boolean ans = isPalindrome(arr[i]);
if (ans == false )
return false ;
}
return true ;
}
public static void main(String args[])
{
int [] arr = { 121 , 131 , 20 };
int n = arr.length;
boolean res = isPalinArray(arr, n);
if (res == true )
System.out.println( "Array is a PalinArray" );
else
System.out.println( "Array is not a PalinArray" );
}
}
|
Python3
def isPalindrome(N):
str1 = "" + str (N)
len1 = len (str1)
for i in range ( int (len1 / 2 )):
if (str1[i] ! = str1[len1 - 1 - i]):
return False
return True
def isPalinArray(arr, n):
for i in range (n):
ans = isPalindrome(arr[i])
if (ans = = False ):
return False
return True
if __name__ = = '__main__' :
arr = [ 121 , 131 , 20 ]
n = len (arr)
res = isPalinArray(arr, n)
if (res = = True ):
print ( "Array is a PalinArray" )
else :
print ( "Array is not a PalinArray" )
|
C#
using System;
class GFG
{
static bool isPalindrome( int N)
{
string str = "" + N;
int len = str.Length;
for ( int i = 0; i < len / 2; i++)
{
if (str[i] != str[len - 1 - i ])
return false ;
}
return true ;
}
static bool isPalinArray( int [] arr, int n)
{
for ( int i = 0; i < n; i++)
{
bool ans = isPalindrome(arr[i]);
if (ans == false )
return false ;
}
return true ;
}
public static void Main()
{
int [] arr = { 121, 131, 20 };
int n = arr.Length;
bool res = isPalinArray(arr, n);
if (res == true )
Console.WriteLine( "Array is a PalinArray" );
else
Console.WriteLine( "Array is not a PalinArray" );
}
}
|
PHP
<?php
function isPalindrome( $N )
{
$str = "" . $N ;
$len = strlen ( $str );
for ( $i = 0; $i < $len / 2; $i ++)
{
if ( $str [ $i ] != $str [ $len - 1 - $i ])
return false;
}
return true;
}
function isPalinArray( $arr , $n )
{
for ( $i = 0; $i < $n ; $i ++)
{
$ans = isPalindrome( $arr [ $i ]);
if ( $ans == false)
return false;
}
return true;
}
$arr = array (121, 131, 20);
$n = sizeof( $arr );
$res = isPalinArray( $arr , $n );
if ( $res == true)
echo "Array is a PalinArray" ;
else
echo "Array is not a PalinArray" ;
?>
|
Javascript
<script>
function isPalindrome(N)
{
let str = "" + N;
let len = str.length;
for (let i = 0; i < parseInt(len / 2, 10); i++)
{
if (str[i] != str[len - 1 - i ])
return false ;
}
return true ;
}
function isPalinArray(arr, n)
{
for (let i = 0; i < n; i++)
{
let ans = isPalindrome(arr[i]);
if (ans == false )
return false ;
}
return true ;
}
let arr = [ 121, 131, 20 ];
let n = arr.length;
let res = isPalinArray(arr, n);
if (res == true )
document.write( "Array is a PalinArray" );
else
document.write( "Array is not a PalinArray" );
</script>
|
Output
Array is not a PalinArray
Complexity Analysis:
- Time Complexity: O(n2) since we need to traverse every nested array in the worst case.
- Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...