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 : 368Input : 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++
// C++ program to calculate the sum of all // palindromic numbers in array #include<bits/stdc++.h> using namespace std; // Function to reverse a number n int reverse( int n) { int d = 0, s = 0; while (n > 0) { d = n % 10; s = s * 10 + d; n = n / 10; } return s; } // Function to check if a number n is // palindrome bool isPalin( int n) { // If n is equal to the reverse of n // it is a palindrome return n == reverse(n); } // Function to calculate sum of all array // elements which are palindrome int sumOfArray( int arr[], int n) { int s = 0; for ( int i = 0; i < n; i++) { if ((arr[i] > 10) && isPalin(arr[i])) { // summation of all palindrome numbers // present in array s += arr[i]; } } return s; } // Driver Code int main() { int n = 6; int arr[] = { 12, 313, 11, 44, 9, 1 }; cout << sumOfArray(arr, n); return 0; } // This code is contributed by mits |
Java
// Java program to calculate the sum of all // palindromic numbers in array class GFG { // Function to reverse a number n 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; } // Function to check if a number n is // palindrome static boolean isPalin( int n) { // If n is equal to the reverse of n // it is a palindrome return n == reverse(n); } // Function to calculate sum of all array // elements which are palindrome static int sumOfArray( int [] arr, int n) { int s = 0 ; for ( int i = 0 ; i < n; i++) { if ((arr[i] > 10 ) && isPalin(arr[i])) { // summation of all palindrome numbers // present in array s += arr[i]; } } return s; } // Driver Code public static void main(String[] args) { int n = 6 ; int [] arr = { 12 , 313 , 11 , 44 , 9 , 1 }; System.out.println(sumOfArray(arr, n)); } } |
C#
// C# program to calculate the sum of all // palindromic numbers in array using System; class GFG { // Function to reverse a number n 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; } // Function to check if a number n is // palindrome static bool isPalin( int n) { // If n is equal to the reverse of n // it is a palindrome return n == reverse(n); } // Function to calculate sum of all array // elements which are palindrome static int sumOfArray( int [] arr, int n) { int s = 0; for ( int i = 0; i < n; i++) { if ((arr[i] > 10) && isPalin(arr[i])) { // summation of all palindrome numbers // present in array s += arr[i]; } } return s; } // Driver Code public static void Main(String[] args) { int n = 6; int [] arr = { 12, 313, 11, 44, 9, 1 }; Console.WriteLine(sumOfArray(arr, n)); } } /* This code contributed by PrinciRaj1992 */ |
Python3
# Python3 program to calculate the sum of all # palindromic numbers in array # Function to reverse a number n def reverse(n) : d = 0 ; s = 0 ; while (n > 0 ) : d = n % 10 ; s = s * 10 + d; n = n / / 10 ; return s; # Function to check if a number n is # palindrome def isPalin(n) : # If n is equal to the reverse of n # it is a palindrome return n = = reverse(n); # Function to calculate sum of all array # elements which are palindrome def sumOfArray(arr, n) : s = 0 ; for i in range (n) : if ((arr[i] > 10 ) and isPalin(arr[i])) : # summation of all palindrome numbers # present in array s + = arr[i]; return s; # Driver Code if __name__ = = "__main__" : n = 6 ; arr = [ 12 , 313 , 11 , 44 , 9 , 1 ]; print (sumOfArray(arr, n)); # This code is contributed by AnkitRai01 |
Javascript
<script> // Javascript program to calculate the // sum of all palindromic numbers in array // Function to reverse a number n 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 to check if a number n is // palindrome function isPalin(n) { // If n is equal to the reverse of n // it is a palindrome return n == reverse(n); } // Function to calculate sum of all array // elements which are palindrome function sumOfArray( arr, n) { let s = 0; for (let i = 0; i < n; i++) { if ((arr[i] > 10) && isPalin(arr[i])) { // Summation of all palindrome // numbers present in array s += arr[i]; } } return s; } // Driver Code let n = 6; let arr = [ 12, 313, 11, 44, 9, 1 ]; document.write(sumOfArray(arr, n)); // This code is contributed by jana_sayantan </script> |
368
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; } // this code is contributed by aditya942003patil |
Java
/*package whatever //do not write package name here */ 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
# Python3 program to implement the approach # This method reverses the characters in a string # in the range [l, r] by swapping characters # from each end one by one 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_) # Driver Code 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 ) # This code is contributed by phasing17 |
C#
// C# code to implement the approach using System; using System.Collections.Generic; class GFG { // Function to reverse the string static string reverse( string str) { // Convert string to char array char [] charArray = str.ToCharArray(); // Char array to store reversed array 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++) { // Convert to string string str = Convert.ToString(array[i]); // Reverse the string string rev = reverse(str); // If string is palindromic // Update sum if (str.Equals(rev) && array[i] > 9) { sum = sum + array[i]; } } // Display result Console.WriteLine(sum); } } // This code is contributed by phasing17 |
Javascript
// JavaScript code to implement the approach 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); // this code is contributed by phasing17 |
368
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++
// C++ program to implement the approach #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; } // This code is contributed by phasing17 |
Java
// Java program to implement the approach 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); } } // This code is contributed by phasing17 |
Python3
# code 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#
// C# program to implement the approach 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); } } // This code is contributed by phasing17 |
Javascript
// JS program to implement the approach 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) // This code is contributed by phasing17 |
368
Time Complexity: O(n)
Auxiliary Space: O(d), where d is the maximum digits in the number.
Contact Us