Compruebe si todos los elementos de la array son palíndromos o no

Dada una array arr[] de N elementos. La tarea es verificar si la array es PalinArray o no, es decir, si todos los elementos de la array son palíndromos o no. 
Ejemplos: 
 

Entrada: arr[] = {121, 131, 20} 
Salida: Array is not a PalinArray 
Para la array dada, el elemento 20 no es un palíndromo. Por lo tanto, la array no es una PalinArray.
Entrada: arr[] = {111, 121, 222, 333, 444} 
Salida: Array is a PalinArray 
Para la array dada, todos los elementos de la array son palíndromos. Por lo tanto, la array es una PalinArray. 
 

Acercarse: 
 

  • Recorre todos los elementos de la array dada y verifica si cada uno es un palíndromo o no.
  • En caso afirmativo, print Array es un PalinArray.
  • De lo contrario, print Array no es un PalinArray.

A continuación se muestra la implementación del enfoque anterior: 
 

C++

// CPP implementation to check
// if an array is PalinArray or not
#include<bits/stdc++.h>
using namespace std;
 
    // Function to check if palindrome or not
    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;
    }
 
    // Function to check
    // if an array is PalinArray or not
    bool isPalinArray(int arr[] , int n)
    {
        // Traversing each element of the array
        // and check if it is palindrome or not
        for (int i = 0; i < n; i++) {
            bool ans = isPalindrome(arr[i]);
            if (ans == false)
                return false;
        }
        return true;
    }
 
    // Driver code
    int main()
    {
        int arr[] = { 121, 131, 20 };
 
        // length of array
        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";
    }
 
// This code is contributed by
// Surendra_Gangwar

Java

// Java implementation to check
// if an array is PalinArray or not
class GFG {
 
    // Function to check if palindrome or not
    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;
    }
 
    // Function to check
    // if an array is PalinArray or not
    static boolean isPalinArray(int[] arr, int n)
    {
        // Traversing each element of the array
        // and check if it is palindrome or not
        for (int i = 0; i < n; i++) {
            boolean ans = isPalindrome(arr[i]);
            if (ans == false)
                return false;
        }
        return true;
    }
 
    // Driver code
    public static void main(String args[])
    {
        int[] arr = { 121, 131, 20 };
 
        // length of array
        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

# Python3 implementation to check
# if an array is PalinArray or not
 
# Function to check if palindrome or not
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
 
# Function to check
# if an array is PalinArray or not
def isPalinArray(arr, n):
     
    # Traversing each element of the array
    # and check if it is palindrome or not
    for i in range(n):
        ans = isPalindrome(arr[i])
        if (ans == False):
            return False
    return True
     
# Driver code
if __name__ == '__main__':
     
    arr = [ 121, 131, 20 ]
     
    # length of array
    n = len(arr)
    res = isPalinArray(arr, n)
    if (res == True):
        print("Array is a PalinArray")
    else:
        print("Array is not a PalinArray")
 
# This code is contributed by PrinciRaj1992

C#

// C# implementation to check
// if an array is PalinArray or not
using System;
 
class GFG
{
 
// Function to check if palindrome or not
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;
}
 
// Function to check if an array is
// PalinArray or not
static bool isPalinArray(int[] arr, int n)
{
    // Traversing each element of the array
    // and check if it is palindrome or not
    for (int i = 0; i < n; i++)
    {
        bool ans = isPalindrome(arr[i]);
        if (ans == false)
            return false;
    }
    return true;
}
 
// Driver code
public static void Main()
{
    int[] arr = { 121, 131, 20 };
 
    // length of array
    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");
}
}
 
// This code is contributed by Ryuga

PHP

<?php
// PHP implementation to check
// if an array is PalinArray or not
 
// Function to check if palindrome or not
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 to check if an array is
// PalinArray or not
function isPalinArray($arr , $n)
{
    // Traversing each element of the array
    // and check if it is palindrome or not
    for ($i = 0; $i < $n; $i++)
    {
        $ans = isPalindrome($arr[$i]);
        if ($ans == false)
            return false;
    }
    return true;
}
 
// Driver code
$arr = array(121, 131, 20);
 
// length of array
$n = sizeof($arr);
 
$res = isPalinArray($arr, $n);
if ($res == true)
    echo "Array is a PalinArray";
else
    echo "Array is not a PalinArray";
 
// This code is contributed by
// Akanksha Rai
?>

Javascript

<script>
    // Javascript implementation to check
    // if an array is PalinArray or not
     
    // Function to check if palindrome or not
    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 to check if an array is
    // PalinArray or not
    function isPalinArray(arr, n)
    {
        // Traversing each element of the array
        // and check if it is palindrome or not
        for (let i = 0; i < n; i++)
        {
            let ans = isPalindrome(arr[i]);
            if (ans == false)
                return false;
        }
        return true;
    }
     
    let arr = [ 121, 131, 20 ];
   
    // length of array
    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");
 
// This code is contributed by decode2207.
</script>
Producción: 

Array is not a PalinArray

 

Análisis de Complejidad:

  • Complejidad de tiempo: O(n 2 ) ya que necesitamos atravesar cada array anidada en el peor de los casos.
  • Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por rachana soma y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *