Largest palindrome which is product of two N | Function to check if a number is a Palindrome or not ; Taking the string value of the number ; Loop to check if every i - th character from beginning is equal to every ( N - i ) th char ; Function to find the largest palindrome which is a product of two N digited numbers ; Find lowerBound , upperBound for a given nDigits . for n = 2 ; [ 10 , 99 ] ; Result variables ; Keep p decrementing by 11 ; Find the nearest number divisible by 11 ; Keep decrementing q by 1 ; Update the result if t > r and is a palindrome ; Prleting the readonly result ; Driver Code
function isPalindrome ( x ) { let num = x . toString ( ) ; let result = true ; let i = 0 ; let j = num . length - 1 ; while ( i < j && result ) { result = num [ i ++ ] == num [ j -- ] ; } return result ; } function find ( nDigits ) { let lowerBound = Math . floor ( Math . pow ( 10 , nDigits - 1 ) ) ; let upperBound = ( lowerBound * 10 ) - 1 ; let resultP = 0 , resultQ = 0 , resultR = 0 ; for ( let p = upperBound ; p > lowerBound ; p -= 11 ) { while ( p % 11 != 0 ) { p -- ; } for ( let q = upperBound ; q > lowerBound ; q -- ) { let t = p * q ; if ( t > resultR && isPalindrome ( t ) ) { resultP = p ; resultQ = q ; resultR = t ; break ; } } } document . write ( resultR ) ; } let N = 2 ; find ( N ) ;