Count the number of digits of palindrome numbers in an array - GeeksforGeeks

10 May, 2021
Given an array arr[] with N integers. The task is to count all the digits of all palindrome numbers present in the array.Examples:  
Input: arr[] = {121, 56, 434} Output: 6 Only 121 and 434 are palindromes and digitCount(121) + digitCount(434) = 3 + 3 = 6Input: arr[] = {56, 455, 546, 234} Output: 0  
 
Approach: For every element of the array, if it is a one digit number then add 1 to the answer for its digit else check if the number is a palindrome. If yes then find the count of its digits and add it to the answer.Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include<bits/stdc++.h>using namespace std; // Function to return the reverse of nint 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 palindromebool isPalin(int n){    return (n == reverse(n));} // Function to return the// count of digits of nint 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 codeint 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 implementation of the approachclass 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 implementation of the approach # Function to return the reverse of ndef 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 palindromedef isPalin(n):    return (n == reverse(n));  # Function to return the# count of digits of ndef 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 codearr = [ 121, 56, 434 ];n = len(arr);print(countPalinDigits(arr, n)); # This code contributed by Rajput-Ji
// C# implementation of the approachusing 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 */
<script> // Javascript implementation of the approach // Function to return the reverse of nfunction 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 palindromefunction isPalin(n){    return (n == reverse(n));} // Function to return the// count of digits of nfunction 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>
6
 
Shorter Python Implementation 
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 codearr = [ 121, 56, 434 ];print(countPalinDigits(arr));
6
 
princiraj1992
Mithun Kumar
Rajput-Ji
mailprakashindia
subhammahato348
number-digits
palindrome
Arrays
Mathematical
Arrays
Mathematical
palindrome
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Window Sliding Technique
Trapping Rain Water
Reversal algorithm for array rotation
Move all negative numbers to beginning and positive to end with constant extra space
Program to find sum of elements in a given array
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7