Suppose we have input n, we have to find the largest palindrome that can be made using multiplication of two n digit numbers. As the numbers are very large, we can perform mod using 1337. So if the input is say 2, then the answer will be 987, 987 = (99*91) mod 1337 = 9009 mod 1337 = 987.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; typedef long long int lli; class Solution { public: int largestPalindrome(int n) { int maxVal = pow(10, n) - 1; int minVal = maxVal / 10; for(int h = maxVal; h > minVal; h--){ lli left = h; lli right = 0; for(lli i = h; i > 0; right = right * 10 + i % 10, left*= 10, i/= 10); lli x = left + right; for(int i = maxVal; i > minVal; i--){ if(i < x / i) break; if(x % i == 0) return x % 1337; } } return 9; } }; main(){ Solution ob; cout << (ob.largestPalindrome(3)); }
3
123