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
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Largest palindrome which is product of two n-digit numbers
- Sum and product of k smallest and k largest composite numbers in the array
- Sum and product of k smallest and k largest prime numbers in the array
- Smallest and Largest Palindrome with N Digits
- Make largest palindrome by changing at most K-digits
- Product of N with its largest odd digit
- Numbers less than N which are product of exactly two distinct prime numbers
- Sum of all N digit palindrome numbers
- Sum of first K even-length Palindrome numbers
- Sum of all palindrome numbers present in an Array
- Find all palindrome numbers of given digits
- Count of N-digit Palindrome numbers
- Sum of all Palindrome Numbers present in a Linked list
- Count the number of digits of palindrome numbers in an array
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Nth term of a sequence formed by sum of current term with product of its largest and smallest digit
- Largest Even and Odd N-digit numbers
- Check whether product of 'n' numbers is even or odd
- Find two numbers with sum and product both same as N
- Smallest and Largest sum of two n-digit numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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 : sapnasingh4991