Count the number of digits of palindrome numbers in an array
Last Updated :
16 Dec, 2022
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 = 6
Input: 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++
// 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
import java.util.*;
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>
Time Complexity : O(n)
Auxiliary Space: O(1)
Shorter Python Implementation
C++
// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the count of digits
// in all the palindromic numbers of arr[]
int countPalinDigits(vector<int> arr)
{
int sum = 0;
for (int n: arr)
{
string n_str = to_string(n);
int l = n_str.length();
string rev = to_string(n);
reverse(rev.begin(), rev.end());
if (rev == n_str) // if palindrome
sum += l;
}
return sum;
}
// Driver code
int main()
{
vector <int> arr = { 121, 56, 434 };
cout << countPalinDigits(arr) << endl;
}
// This code is contributed by phasing17
Java
// Java code to implement the approach
import java.util.*;
class GFG
{
// Function to return the count of digits
// in all the palindromic numbers of arr[]
static int countPalinDigits(int[] arr)
{
int sum = 0;
for (int n : arr)
{
String n_str = String.valueOf(n);
int l = n_str.length();
String rev = new StringBuilder(new String(n_str)).reverse().toString();
if (rev.equals(n_str)) // if palindrome
sum += l;
}
return sum;
}
// Driver code
public static void main(String[] args)
{
int[] arr = { 121, 56, 434 };
System.out.println(countPalinDigits(arr));
}
}
// This code is contributed by phasing17
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));
C#
// C# code to implement the approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to return the count of digits
// in all the palindromic numbers of arr[]
static int countPalinDigits(int[] arr)
{
int sum = 0;
foreach (int n in arr)
{
string n_str = Convert.ToString(n);
int l = n_str.Length;
string rev = Convert.ToString(n);
char[] revs = rev.ToCharArray();
Array.Reverse(revs);
rev = new string(revs);
if (rev.Equals(n_str)) // if palindrome
sum += l;
}
return sum;
}
// Driver code
public static void Main(string[] args)
{
int[] arr = { 121, 56, 434 };
Console.WriteLine(countPalinDigits(arr));
}
}
// This code is contributed by phasing17
JavaScript
// Function to return the count of digits
// in all the palindromic numbers of arr[]
function countPalinDigits(arr)
{
let sum = 0
for (let n of arr)
{
let n_str = "" + n;
let l = n_str.length
let rev = n_str.split("").reverse().join("")
if (rev.localeCompare(n_str) == 0) // if palindrome
sum += l
}
return sum
}
// Driver code
let arr = [ 121, 56, 434 ];
console.log(countPalinDigits(arr));
// This code is contributed by phasing17
Time Complexity : O(n)
Auxiliary Space: O(n)
Similar Reads
Count of N-digit Palindrome numbers Given an integer N, the task is to find the count of N-digit Palindrome numbers.Examples: Input: N = 1 Output: 9 {1, 2, 3, 4, 5, 6, 7, 8, 9} are all the possible single digit palindrome numbers.Input: N = 2 Output: 9 Approach: The first digit can be any of the 9 digits (not 0) and the last digit wil
2 min read
Check if a number with even number of digits is palindrome or not Given a number N containing an even number of digits. The task is to check whether that number is palindrome or not. Examples: Input: N = 123321 Output: Palindrome Input: 1234 Output: Not palindrome A Naive Approach is to traverse from the front and back of that number and stop where they do not mat
4 min read
Count of distinct numbers formed by shuffling the digits of a large number N Given a large number N in the form of a string, the task is to determine the count of distinct numbers that can be formed by shuffling the digits of the number N. Note: N may contain leading zeros. The number itself is also taken into count.Since the answer could be very large, print result modulo 1
11 min read
Count of N digit palindromic numbers divisible by 9 Given an integer N, the task is to count the number of N digit palindromic numbers containing digits from 1 to 9 and divisible by 9. Examples: Input: N = 1 Output: 1 Explanation: Only 9 is 1 digit number which is palindrome and divisible by 9.Input: N = 3 Output: 9 Explanation: Three digit numbers t
4 min read
Queries to count Palindrome Numbers from a range whose sum of digits is a Prime Number Given an array Q[][] consisting of N queries of the form {L, R}, the task for each query is to find the count of the numbers in the range [L, R] that are palindrome and the sum of their digits is a prime number. Examples: Input: Q[][] = {{5, 9}, {5, 22}}Output:23Explanation:Query 1: All palindrome n
14 min read