Cuente el número de dígitos de los números palíndromos en una array

Dada una array arr[] con N enteros. La tarea es contar todos los dígitos de todos los números palíndromos presentes en la array.
Ejemplos: 
 

Entrada: arr[] = {121, 56, 434} 
Salida:
Solo 121 y 434 son palíndromos 
y digitCount(121) + digitCount(434) = 3 + 3 = 6
Entrada: arr[] = {56, 455, 546 , 234} 
Salida:
 

Enfoque: para cada elemento de la array, si es un número de un dígito, agregue 1 a la respuesta para su dígito; de lo contrario, verifique si el número es un palíndromo. En caso afirmativo, encuentre el recuento de sus dígitos y agréguelo a la respuesta.
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation of the approach
#include<bits/stdc++.h>
using namespace std;
 
// Function to return the reverse of n
int reverse(int n)
{
    int rev = 0;
    while (n > 0)
    {
        int d = n % 10;
        rev = rev * 10 + d;
        n = n / 10;
    }
    return rev;
}
 
// Function that returns true
// if n is a palindrome
bool isPalin(int n)
{
    return (n == reverse(n));
}
 
// Function to return the
// count of digits of n
int countDigits(int n)
{
    int c = 0;
    while (n > 0)
    {
        n = n / 10;
        c++;
    }
    return c;
}
 
// Function to return the count of digits
// in all the palindromic numbers of arr[]
int countPalinDigits(int arr[], int n)
{
    int s = 0;
    for (int i = 0; i < n; i++)
    {
 
        // If arr[i] is a one digit number
        // or it is a palindrome
        if (arr[i] < 10 || isPalin(arr[i]))
        {
            s += countDigits(arr[i]);
        }
    }
    return s;
}
 
// Driver code
int main()
{
    int arr[] = { 121, 56, 434 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << (countPalinDigits(arr, n));
    return 0;
}
 
// This code is contributed by mits

Java

// Java implementation of the approach
class GFG {
 
    // Function to return the reverse of n
    static int reverse(int n)
    {
        int rev = 0;
        while (n > 0) {
            int d = n % 10;
            rev = rev * 10 + d;
            n = n / 10;
        }
        return rev;
    }
 
    // Function that returns true
    // if n is a palindrome
    static boolean isPalin(int n)
    {
        return (n == reverse(n));
    }
 
    // Function to return the
    // count of digits of n
    static int countDigits(int n)
    {
        int c = 0;
        while (n > 0) {
            n = n / 10;
            c++;
        }
        return c;
    }
 
    // Function to return the count of digits
    // in all the palindromic numbers of arr[]
    static int countPalinDigits(int[] arr, int n)
    {
        int s = 0;
        for (int i = 0; i < n; i++) {
 
            // If arr[i] is a one digit number
            // or it is a palindrome
            if (arr[i] < 10 || isPalin(arr[i])) {
                s += countDigits(arr[i]);
            }
        }
        return s;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int[] arr = { 121, 56, 434 };
        int n = arr.length;
        System.out.println(countPalinDigits(arr, n));
    }
}

Python3

# Python3 implementation of the approach
 
# Function to return the reverse of n
def reverse(n):
    rev = 0;
    while (n > 0):
        d = n % 10;
        rev = rev * 10 + d;
        n = n // 10;
    return rev;
 
# Function that returns true
# if n is a palindrome
def isPalin(n):
    return (n == reverse(n));
 
 
# Function to return the
# count of digits of n
def countDigits(n):
    c = 0;
    while (n > 0):
        n = n // 10;
        c += 1;
    return c;
 
# Function to return the count of digits
# in all the palindromic numbers of arr[]
def countPalinDigits(arr, n):
    s = 0;
    for i in range(n):
 
        # If arr[i] is a one digit number
        # or it is a palindrome
        if (arr[i] < 10 or isPalin(arr[i])):
            s += countDigits(arr[i]);
 
    return s;
 
 
# Driver code
arr = [ 121, 56, 434 ];
n = len(arr);
print(countPalinDigits(arr, n));
 
# This code contributed by Rajput-Ji

C#

// C# implementation of the approach
using System;
     
class GFG
{
 
    // Function to return the reverse of n
    static int reverse(int n)
    {
        int rev = 0;
        while (n > 0)
        {
            int d = n % 10;
            rev = rev * 10 + d;
            n = n / 10;
        }
        return rev;
    }
 
    // Function that returns true
    // if n is a palindrome
    static bool isPalin(int n)
    {
        return (n == reverse(n));
    }
 
    // Function to return the
    // count of digits of n
    static int countDigits(int n)
    {
        int c = 0;
        while (n > 0)
        {
            n = n / 10;
            c++;
        }
        return c;
    }
 
    // Function to return the count of digits
    // in all the palindromic numbers of arr[]
    static int countPalinDigits(int[] arr, int n)
    {
        int s = 0;
        for (int i = 0; i < n; i++)
        {
 
            // If arr[i] is a one digit number
            // or it is a palindrome
            if (arr[i] < 10 || isPalin(arr[i]))
            {
                s += countDigits(arr[i]);
            }
        }
        return s;
    }
 
    // Driver code
    public static void Main()
    {
        int[] arr = { 121, 56, 434 };
        int n = arr.Length;
        Console.WriteLine(countPalinDigits(arr, n));
    }
}
 
/* This code contributed by PrinciRaj1992 */

Javascript

<script>
 
// Javascript implementation of the approach
 
// Function to return the reverse of n
function reverse(n)
{
    let rev = 0;
    while (n > 0)
    {
        let d = n % 10;
        rev = rev * 10 + d;
        n = parseInt(n / 10);
    }
    return rev;
}
 
// Function that returns true
// if n is a palindrome
function isPalin(n)
{
    return (n == reverse(n));
}
 
// Function to return the
// count of digits of n
function countDigits(n)
{
    let c = 0;
    while (n > 0)
    {
        n = parseInt(n / 10);
        c++;
    }
    return c;
}
 
// Function to return the count of digits
// in all the palindromic numbers of arr[]
function countPalinDigits(arr, n)
{
    let s = 0;
    for (let i = 0; i < n; i++)
    {
 
        // If arr[i] is a one digit number
        // or it is a palindrome
        if (arr[i] < 10 || isPalin(arr[i]))
        {
            s += countDigits(arr[i]);
        }
    }
    return s;
}
 
// Driver code
    let arr = [ 121, 56, 434 ];
    let n = arr.length;
    document.write(countPalinDigits(arr, n));
 
</script>
Producción: 

6

 

Complejidad de tiempo : O(n)

Espacio Auxiliar: O(1)

Implementación de Python más corta
 

Python3

# Function to return the count of digits
# in all the palindromic numbers of arr[]
def countPalinDigits(arr):
   sum = 0
  
   for n in arr:
      n_str = str(n)
      l = len(n_str)
      if n_str[l::-1] == n_str: # if palindrome
         sum += l
   return sum
 
# Driver code
arr = [ 121, 56, 434 ];
print(countPalinDigits(arr));
Producción: 

6

 

Complejidad de tiempo : O(n)

Espacio Auxiliar: O(n)

Publicación traducida automáticamente

Artículo escrito por facebookruppal 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 *