Sum of all palindrome numbers present in an Array
Given an array arr[] of N positive integers. The task is to find the sum of all palindrome numbers present in the array. Print the total sum.
A palindrome number is a number that when reversed is equal to the initial number. Example: 121 is palindrome(reverse(121) = 121), 123 is not palindrome(reverse(123) = 321).
Note: Consider palindrome numbers of length greater than 1 while calculating the sum.
Examples:
Input : arr[] ={12, 313, 11, 44, 9, 1}
Output : 368
Input : arr[] = {12, 11, 121}
Output : 132
Approach: The idea is to implement a reverse function that reverses a number from the right to left. Implement a function that checks for palindrome numbers and finally traverses the array and calculates the sum of all elements which are palindrome.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int reverse( int n)
{
int d = 0, s = 0;
while (n > 0)
{
d = n % 10;
s = s * 10 + d;
n = n / 10;
}
return s;
}
bool isPalin( int n)
{
return n == reverse(n);
}
int sumOfArray( int arr[], int n)
{
int s = 0;
for ( int i = 0; i < n; i++)
{
if ((arr[i] > 10) && isPalin(arr[i]))
{
s += arr[i];
}
}
return s;
}
int main()
{
int n = 6;
int arr[] = { 12, 313, 11, 44, 9, 1 };
cout << sumOfArray(arr, n);
return 0;
}
|
Java
class GFG {
static int reverse( int n)
{
int d = 0 , s = 0 ;
while (n > 0 ) {
d = n % 10 ;
s = s * 10 + d;
n = n / 10 ;
}
return s;
}
static boolean isPalin( int n)
{
return n == reverse(n);
}
static int sumOfArray( int [] arr, int n)
{
int s = 0 ;
for ( int i = 0 ; i < n; i++) {
if ((arr[i] > 10 ) && isPalin(arr[i])) {
s += arr[i];
}
}
return s;
}
public static void main(String[] args)
{
int n = 6 ;
int [] arr = { 12 , 313 , 11 , 44 , 9 , 1 };
System.out.println(sumOfArray(arr, n));
}
}
|
C#
using System;
class GFG
{
static int reverse( int n)
{
int d = 0, s = 0;
while (n > 0)
{
d = n % 10;
s = s * 10 + d;
n = n / 10;
}
return s;
}
static bool isPalin( int n)
{
return n == reverse(n);
}
static int sumOfArray( int [] arr, int n)
{
int s = 0;
for ( int i = 0; i < n; i++)
{
if ((arr[i] > 10) && isPalin(arr[i]))
{
s += arr[i];
}
}
return s;
}
public static void Main(String[] args)
{
int n = 6;
int [] arr = { 12, 313, 11, 44, 9, 1 };
Console.WriteLine(sumOfArray(arr, n));
}
}
|
Python3
def reverse(n) :
d = 0 ; s = 0 ;
while (n > 0 ) :
d = n % 10 ;
s = s * 10 + d;
n = n / / 10 ;
return s;
def isPalin(n) :
return n = = reverse(n);
def sumOfArray(arr, n) :
s = 0 ;
for i in range (n) :
if ((arr[i] > 10 ) and isPalin(arr[i])) :
s + = arr[i];
return s;
if __name__ = = "__main__" :
n = 6 ;
arr = [ 12 , 313 , 11 , 44 , 9 , 1 ];
print (sumOfArray(arr, n));
|
Javascript
<script>
function reverse( n)
{
let d = 0, s = 0;
while (n > 0)
{
d = n % 10;
s = s * 10 + d;
n = Math.floor(n / 10);
}
return s;
}
function isPalin(n)
{
return n == reverse(n);
}
function sumOfArray( arr, n)
{
let s = 0;
for (let i = 0; i < n; i++)
{
if ((arr[i] > 10) && isPalin(arr[i]))
{
s += arr[i];
}
}
return s;
}
let n = 6;
let arr = [ 12, 313, 11, 44, 9, 1 ];
document.write(sumOfArray(arr, n));
</script>
|
Time Complexity: O(n * max(arr)), where max(arr) is the largest element of the array arr.
Auxiliary Space: O(1), since no extra space has been taken.
Another approach :
In java, we can easily implement it by using StringBuilder object and the reverse() method.
Step 1: Get the input from the user
Step 2: Initialize sum=0 and iterate through each element.
Step 3: Now, convert the integer element to string by using Integer.toString(array[i])
Step 4:Reverse it by StringBuilder object using reverse() method and toString() is used to convert the object to string.
Step 5: Equalize both the string values and check element greater than 9. If it satisfies the condition, sum the elements.
C++
#include <bits/stdc++.h>
using namespace std;
string reverse(string str, int l, int r)
{
while (l<r)
{
swap(str[l++],str[r--]);
}
return str;
}
int main()
{
int array[]={12, 313, 11, 44, 9, 1};
int n = sizeof (array)/ sizeof (array[0]);
int sum = 0;
for ( int i = 0; i < n; i++){
string str = to_string(array[i]);
string rev=reverse(str,0,str.size()-1);
if (str == rev && array[i] > 9){
sum=sum+array[i];
}
}
cout << sum;
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main (String[] args) {
int array[]={ 12 , 313 , 11 , 44 , 9 , 1 };
int n=array.length;
int sum= 0 ;
for ( int i= 0 ;i<n;i++){
String str=Integer.toString(array[i]);
String rev= new StringBuilder(str).reverse().toString();
if (str.equals(rev) && array[i]> 9 ){
sum=sum+array[i];
}
}
System.out.println(sum);
}
}
|
Python3
def reverse(str_, l, r):
str_ = list (str_)
while l < r:
str_[i], str_[j] = str_[j], str_[i]
l + = 1
r - = 1
return "".join(str_)
array = [ 12 , 313 , 11 , 44 , 9 , 1 ]
n = len (array)
sum = 0
for i in range (n):
string = str (array[i])
rev = string[:: - 1 ]
if string = = rev and array[i] > 9 :
sum + = array[i]
print ( sum )
|
C#
using System;
using System.Collections.Generic;
class GFG {
static string reverse( string str)
{
char [] charArray = str.ToCharArray();
char [] result = new char [charArray.Length];
for ( int i = 0, j = str.Length - 1; i < str.Length;
i++, j--) {
result[i] = charArray[j];
}
return new string (result);
}
public static void Main( string [] args)
{
int [] array = { 12, 313, 11, 44, 9, 1 };
int n = array.Length;
int sum = 0;
for ( int i = 0; i < n; i++) {
string str = Convert.ToString(array[i]);
string rev = reverse(str);
if (str.Equals(rev) && array[i] > 9) {
sum = sum + array[i];
}
}
Console.WriteLine(sum);
}
}
|
Javascript
function reverse(str, l, r)
{
str = str.split( "" );
while (l < r) {
let temp = str[r];
str[r--] = str[l];
str[l++] = temp;
}
return str.join( "" );
}
let array = [ 12, 313, 11, 44, 9, 1 ];
let n = array.length;
let sum = 0;
for (let i = 0; i < n; i++) {
let str = "" + (array[i]);
let rev = reverse(str, 0, str.length - 1);
if (str == rev && array[i] > 9) {
sum = sum + array[i];
}
}
console.log(sum);
|
Time Complexity: O(n), where n is the size of the given array.
Auxiliary Space: O(d), where d is the maximum digits in number in the given array.
In python, we can execute it by implementing the below approach.
Step 1 : Initialize the list or array and sum=0.
Step 2 : Iterate the list using for loop and convert the integer element to string using str().
Step 3 : Reverse the string using string_element[ : : -1].
Step 4 :Equalize both the string values and check element greater than 9. If it satisfies the condition, sum the elements.
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int lis[] = {12, 313, 11, 44, 9, 1};
int sum = 0;
for ( int i : lis)
{
string string_conversion = to_string(i);
string rev_string = "" + string_conversion;
reverse(rev_string.begin(), rev_string.end());
if (string_conversion == (rev_string) && i>9)
sum = sum + i;
}
cout << sum;
}
|
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
int lis[] = { 12 , 313 , 11 , 44 , 9 , 1 };
int sum = 0 ;
for ( int i : lis) {
String string_conversion = String.valueOf(i);
StringBuilder rev
= new StringBuilder(string_conversion);
rev.reverse();
String rev_string = rev.toString();
if (string_conversion.equals(rev_string)
&& i > 9 )
sum = sum + i;
}
System.out.println(sum);
}
}
|
Python3
lis = [ 12 , 313 , 11 , 44 , 9 , 1 ]
sum = 0 ;
for i in lis:
string_conversion = str (i)
rev_string = string_conversion[ : : - 1 ]
if (string_conversion = = rev_string and i> 9 ):
sum = sum + i
print ( sum )
|
C#
using System;
using System.Collections.Generic;
class GFG {
public static void Main( string [] args)
{
int [] lis = { 12, 313, 11, 44, 9, 1 };
int sum = 0;
foreach ( int i in lis)
{
string string_conversion = Convert.ToString(i);
char [] rev = string_conversion.ToCharArray();
Array.Reverse(rev);
string rev_string = new string (rev);
if (string_conversion.Equals(rev_string)
&& i > 9)
sum = sum + i;
}
Console.WriteLine(sum);
}
}
|
Javascript
let lis = [12, 313, 11, 44, 9, 1]
let sum = 0;
for ( var i of lis)
{
let string_conversion = "" + i
let rev_string = string_conversion.split( "" ).reverse().join( "" );
if ((string_conversion.localeCompare(rev_string) == 0) && i>9)
sum = sum + i
}
console.log(sum)
|
Time Complexity: O(n)
Auxiliary Space: O(d), where d is the maximum digits in the number.
Last Updated :
27 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...