Largest palindromic number in an array | Function to check if n is palindrome ; Find the appropriate divisor to extract the leading digit ; If first and last digits are not same then return false ; Removing the leading and trailing digits from the number ; Reducing divisor by a factor of 2 as 2 digits are dropped ; Function to find the largest palindromic number ; If a palindrome larger than the currentMax is found ; Return the largest palindromic number from the array ; Driver Code ; print required answer
< ? php function isPalindrome ( $ n ) { $ divisor = 1 ; while ( ( int ) ( $ n / $ divisor ) >= 10 ) $ divisor *= 10 ; while ( $ n != 0 ) { $ leading = ( int ) ( $ n / $ divisor ) ; $ trailing = $ n % 10 ; if ( $ leading != $ trailing ) return false ; $ n = ( $ n % $ divisor ) / 10 ; $ divisor = $ divisor / 100 ; } return true ; } function largestPalindrome ( $ A , $ n ) { $ currentMax = -1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ A [ $ i ] > $ currentMax && isPalindrome ( $ A [ $ i ] ) ) $ currentMax = $ A [ $ i ] ; } return $ currentMax ; } $ A = array ( 1 , 232 , 54545 , 999991 ) ; $ n = sizeof ( $ A ) ; echo ( largestPalindrome ( $ A , $ n ) ) ; ? >