Largest Palindrome Product – Leetcode

Problem

Find the largest palindrome made from the product of two n-digit numbers. Since the result could be very large, you should return the largest palindrome mod 1337.

Example:

Input: 3  ,  Output: 987

Explanation:

99 x 91 = 9009, 9009 % 1337 = 987

Note:  The range of n is [1,8].

Solution

<br />
package com.programtalk.learn.interview.questions;</p>
<p>public class LargestPalindromeProduct {<br />
	public static void main(String[] args) {<br />
		for (int i = 2; i &lt; 9; i++) {<br />
			System.out.println(&quot;input: &quot; + i);<br />
			int largestPalindrome = new LargestPalindromeProduct().largestPalindrome(i);<br />
			System.out.println(&quot;plaindrome % 1337 is : &quot; + largestPalindrome + &quot;\n\n&quot;);</p>
<p>		}<br />
	}</p>
<p>	public int largestPalindrome(int n) {<br />
		if (n == 1) {<br />
			return 9;<br />
		}<br />
		// if n = 3 then max = 999<br />
		int upperBound = (int) Math.pow(10, n) - 1;<br />
		int lowerBound = upperBound / 10;<br />
		for (int v = upperBound - 1; v &gt; lowerBound; v--) {<br />
			// creates maximum assumed palindrom<br />
			// e.g. if n = 3 first time the maximum assumed palindrom will be<br />
			// 998 899<br />
			long palindrome = Long.valueOf(v + new StringBuilder().append(v).reverse().toString());<br />
			for (long x = upperBound; x * x &gt;= palindrome; x--) {<br />
				if (palindrome % x == 0) {<br />
					System.out.println(&quot;numbers multiplied : &quot; + x + &quot; * &quot; + palindrome / x);<br />
					System.out.println(&quot;palindrome :&quot; + palindrome);<br />
					return (int) (palindrome % 1337);<br />
				}<br />
			}<br />
		}<br />
		return 0;<br />
	}<br />
}<br />

And if don’t like to use StringBuilder.reverse() then you can use the below function to reverse the number

<br />
	public int reverseInt(int input) {<br />
		long reversedNum = 0;</p>
<p>		long input_long = input;</p>
<p>		while (input_long != 0) {<br />
			reversedNum = reversedNum * 10 + input_long % 10;<br />
			input_long = input_long / 10;<br />
		}</p>
<p>		if (reversedNum &gt; Integer.MAX_VALUE || reversedNum &lt; Integer.MIN_VALUE) {<br />
			throw new IllegalArgumentException();<br />
		}<br />
		return (int) reversedNum;<br />
	}</p>
<p>

Output

</p>
<p>input: 2<br />
numbers multiplied : 99 * 91<br />
palindrome :9009<br />
plaindrome % 1337 is : 987</p>
<p>input: 3<br />
numbers multiplied : 993 * 913<br />
palindrome :906609<br />
plaindrome % 1337 is : 123</p>
<p>input: 4<br />
numbers multiplied : 9999 * 9901<br />
palindrome :99000099<br />
plaindrome % 1337 is : 597</p>
<p>input: 5<br />
numbers multiplied : 99979 * 99681<br />
palindrome :9966006699<br />
plaindrome % 1337 is : 677</p>
<p>input: 6<br />
numbers multiplied : 999999 * 999001<br />
palindrome :999000000999<br />
plaindrome % 1337 is : 1218</p>
<p>input: 7<br />
numbers multiplied : 9998017 * 9997647<br />
palindrome :99956644665999<br />
plaindrome % 1337 is : 877</p>
<p>input: 8<br />
numbers multiplied : 99999999 * 99990001<br />
palindrome :9999000000009999<br />
plaindrome % 1337 is : 475</p>
<p>

Like this post? Don’t forget to share it!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.