Tuesday, April 14, 2015

HackerRank::Contests::ProjectEuler::Project Euler #4: Largest palindrome product

   Precalculating all the numbers before processing the input , is a very good idea. There are 900x900 possible multiplications, Checking if the number is a palindrome or not is not a hard job, there are various methods for that, you can see my method in the source code below. So, while multiplying we need to take care of the repeating numbers, for that you can simple keep a boolean array B of a length of 1 million, where each B[i] will show whether the number i has been already seen or not. Every time , when you calculate the current multiplication (suppose that number is M), check for B[M], it it's true than just continue to the next pair , otherwise mark it as true and process it. Store all the found palindromes in an array and sort them. After that, processing the input is simply a piece of cake when you have all the numbers calculated and stored in an increasing order.



No comments:

Post a Comment