Given a value N, find out the largest palindrome number which is the product of two N-digit numbers.
Input: N = 2
9009 is the largest number which is product of two 2-digit numbers 91 and 99 (9009 = 91*99)
Input: N = 3
Input: N = 4
Observation: The following observation can be made for the above problem:
Let N = 2, then the product will contain 4 digits. Since the product will be a palindrome it will be of the form “abba” where a, b are digits at their respective place value.
For N = 2:
“abba” = 1000a + 100b + 10b + a
= 1001a + 110b
= 11.(91a + 10b)
Similarly, for N = 3:
“abccba” = 100000a + 10000b + 1000c + 100c + 10b + 1a
= 100001a + 10010b + 1100c
= 11.(9091a + 910b + 100c)
For N = 5:
“abcdeedcba” = 1000000000a + 100000000b + 10000000c + 1000000d + 100000e + 10000e + 1000d + 100c + 10b + a
= 1000000001a + 100000010b + 10000100c + 100100d + 110000e
= 11.(90909091a + 909091b + 91000c + 10000d)
Approach: From the above observation, a pattern can be observed that every palindrome product will always have a factor 11.
- For any N digit numbers P and Q, if the product of P and Q is a palindrome, then either P or Q is divisible by 11 but not both of them.
- Therefore, instead of checking whether the product of P and Q is a palindrome for all the possible pairs of P and Q, we can reduce the number of computations by checking only for the multiples of 11 for one of the numbers.
Below is the implementation of the above approach:
Related Article: Largest palindrome which is product of two n-digit numbers
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Largest palindrome which is product of two n-digit numbers
- Count all palindrome which is square of a palindrome
- Numbers less than N which are product of exactly two distinct prime numbers
- Given two strings check which string makes a palindrome first
- Sentence Palindrome (Palindrome after removing spaces, dots, .. etc)
- Sum and product of k smallest and k largest prime numbers in the array
- Sum and product of k smallest and k largest composite numbers in the array
- Program for dot product and cross product of two vectors
- Count numbers which can be constructed using two numbers
- Largest number by which given 3 numbers should be divided such that they leaves same remainder
- Largest number in [2, 3, .. n] which is co-prime with numbers in [2, 3, .. m]
- Find the length of largest subarray in which all elements are Autobiographical Numbers
- Make largest palindrome by changing at most K-digits
- Smallest and Largest Palindrome with N Digits
- Sum and Product of all Composite numbers which are divisible by k in an array
- Check if there exists any sub-sequence in a string which is not palindrome
- Count of three non-overlapping sub-strings which on concatenation forms a palindrome
- Largest of two distinct numbers without using any conditional statements or operators
- Smallest and Largest sum of two n-digit numbers
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.