Sum of first K even-length Palindrome numbers
Given a integer k, find the sum of first k even-length palindrome numbers.
Even length here refers to the number of digits of a number is even.
Examples:
Input : k = 3 Output : 66 Explanation: 11 + 22 + 33 = 66 (Sum of first three even-length palindrome numbers) Input : 10 Output : 1496 Explanation: 11+22+33+44+55+66+77+88+ 99+1001 = 1496
A naive approach will be to check every even length number, if it is a palindrome number then we sum it up. We repeat the same process for first K even length palindrome numbers and sum them up to get the sum.
In this case complexity will go high as even length numbers are from 10-99 and then 1000-9999 and then so on…
10-99, 1000-9999, 100000-999999.. has 9, 90, 900 respectively palindrome numbers in them, so to check k numbers we have to check a lot of numbers which will not be efficient enough.
An efficient approach will be to observe a pattern for even length prime numbers.
11, 22, 33, 44, 55, 66, 77, 88, 99, 1001, 1111, 1221, 1331, 1441, 1551, 1661…
1st number is 11, 2nd is 22, third is 33, 16th is 16-rev(16) i.e., 1661.
So the Nth number will int(string(n)+rev(string(n)).
See here for conversion of integer to string and string to integer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h> #include <boost/lexical_cast.hpp> using namespace std; // function to return the sum of // first K even length palindrome numbers int sum(int k) { // loop to get sum of first K even // palindrome numbers int sum = 0; for (int i = 1; i <= k; i++) { // convert integer to string string num = to_string(i); // Find reverse of num. string revNum = num; reverse(revNum.begin(), revNum.end()); // string(n)+rev(string(n) string strnum = (num + revNum); // convert string to integer int number = boost::lexical_cast<int>(strnum); sum += number; // summation } return sum; } // driver program to check the above function int main() { int k = 3; cout << sum(k); return 0; } |
Java
// Java implementation to find sum of // first K even-length Palindrome numbers import java.util.*; import java.lang.*; public class GfG{ public static String reverseString(String str) { StringBuilder sb = new StringBuilder(str); sb.reverse(); return sb.toString(); } // function to return the sum of // first K even length palindrome numbers static int sum(int k) { // loop to get sum of first K even // palindrome numbers int sum = 0; for (int i = 1; i <= k; i++) { // convert integer to string String num = Integer.toString(i); // Find reverse of num. String revNum = num; revNum = reverseString(num); // string(n)+rev(string(n) String strnum = (num + revNum); // convert string to integer int number = Integer.parseInt(strnum); sum += number; // summation } return sum; } // driver function public static void main(String argc[]) { int n = 3; System.out.println(sum(n)); } } // This code is contributed by Prerna Saini |
C#
// C# implementation to find sum of // first K even-length Palindrome numbers using System; class GfG { // function to return the sum of // first K even length palindrome numbers static int sum(int k) { // loop to get sum of first K even // palindrome numbers int sum = 0; for (int i = 1; i <= k; i++) { // convert integer to string String num = Convert.ToString(i); // Find reverse of num. String revNum = num; revNum = reverse(num); // string(n)+rev(string(n) String strnum = (num + revNum); // convert string to integer int number = Convert.ToInt32(strnum); sum += number; // summation } return sum; } static String reverse(String input) { char[] temparray = input.ToCharArray(); int left, right = 0; right = temparray.Length - 1; for (left = 0; left < right; left++, right--) { // Swap values of left and right char temp = temparray[left]; temparray[left] = temparray[right]; temparray[right] = temp; } return String.Join("",temparray); } // Driver code public static void Main(String []argc) { int n = 3; Console.WriteLine(sum(n)); } } // This code is contributed by PrinciRaj1992 |
Output:
66
Recommended Posts:
- Find all palindrome numbers of given digits
- Sum of all palindrome numbers present in an Array
- Count of N-digit Palindrome numbers
- Count the number of digits of palindrome numbers in an array
- Sentence Palindrome (Palindrome after removing spaces, dots, .. etc)
- Count all palindrome which is square of a palindrome
- Nth Even length Palindrome
- N'th palindrome of K digits
- Palindrome Partitioning | DP-17
- Smallest Palindrome after replacement
- Palindrome by swapping only one character
- Find next palindrome prime
- Next word that does not contain a palindrome and has characters from first k
- Palindrome Substring Queries
- Double Base Palindrome
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : princiraj1992



