DDSA Solutions

479. Largest Palindrome Product

Time: O(10^n)
Space: O(1)
Advertisement

Intuition

Find the largest palindrome made from the product of two n-digit numbers. A palindrome can be expressed as upper*10^n + reverse(upper). Iterate upper from largest down.

Algorithm

  1. 1For n=1, return 9.
  2. 2Max = 10^n - 1 (largest n-digit number).
  3. 3For each candidate palindrome (constructed from upper half), check if it has two n-digit factors.
  4. 4Return result mod 1337.

Common Pitfalls

  • This requires careful construction; use long arithmetic to avoid overflow.
479.cs
C#
// Approach: For each n-digit number as the left half, mirror it to create
// a palindrome candidate and check if it has an n-digit divisor.
// Time: O(10^n) Space: O(1)

public class Solution
{
    public int LargestPalindrome(int n)
    {
        if (n == 1)
            return 9;

        const int kMod = 1337;
        int upper = (int)Math.Pow(10, n) - 1;
        int lower = (int)Math.Pow(10, n - 1) - 1;

        for (int i = upper; i > lower; --i)
        {
            long cand = GetPalindromeCandidate(i);
            for (long j = upper; j * j >= cand; --j)
            {
                if (cand % j == 0)
                    return (int)(cand % kMod);
            }
        }

        throw new ArgumentException();
    }

    private long GetPalindromeCandidate(int i)
    {
        string reversed = new string(i.ToString().ToCharArray().Reverse().ToArray());
        return long.Parse(i + reversed);
    }
}
Advertisement
Was this solution helpful?